2.1.1—线性表—Remove Duplicates from Sorted Array
来源:互联网 发布:青岛乐智网络 编辑:程序博客网 时间:2024/06/07 13:04
描述
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
#include<iostream>using namespace std;int DeleteDuplicate(int a[], int n){if (a == NULL || n <= 0)return -1;int i = 0;int j = 1;for (; j < n;){if (a[i] != a[j]){i++;a[i]= a[j];j++;}else{while (a[i] == a[j])j++;}}return i + 1;}int main(){const int n = 10;int a[n] = { 1,1, 2,2,2,2, 3,3,3, 5 };int k = DeleteDuplicate(a, n);for (int i = 0; i < k; i++)cout << a[i] << " ";cout << endl;}
阅读全文
0 0
- 2.1.1—线性表—Remove Duplicates from Sorted Array
- 2.1.2—线性表—Remove Duplicates from Sorted Array II
- 2.1.1 Remove Duplicates from Sorted Array
- 2.1.1 Remove Duplicates from Sorted Array
- LeetCode—Remove Duplicates from Sorted Array
- Leetcode 线性表 Remove Duplicates from Sorted Array
- Leetcode 线性表 Remove Duplicates from Sorted Array II
- 线性表-26.Remove Duplicates from Sorted Array
- leetcode——26. Remove Duplicates from Sorted Array && 80. Remove Duplicates from Sorted Array II
- 1: Remove Duplicates from Sorted Array
- LeetCode 2.1.1 Remove Duplicates from Sorted Array
- LeetCode——Remove Duplicates from Sorted Array
- LeetCode——Remove Duplicates from Sorted Array
- LeetCode26——Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array ——解题报告
- LeetCode——Remove Duplicates from Sorted Array
- leetcode 026 —— Remove Duplicates from Sorted Array
- LeetCode题解——Remove Duplicates from Sorted Array
- 栈的基本操作
- Bootstrap基础7——按钮组button
- FatFs文件系统创建文件夹,在文件夹里创建文件
- 【CS231N】Numpy 使用入门 (3)
- eclipse使用技巧
- 2.1.1—线性表—Remove Duplicates from Sorted Array
- 《gradle 用户指南》中文版 第一部分、关于Gradle
- 进程和线程的区别与联系
- JS有关日期的基本操作
- 简化代码
- 【JAVA微信开发】WxMessageSDK-轻量的微信公众号消息处理SDK
- 深入理解Java虚拟机 第七章 虚拟机类加载器
- Maven聚合模块与继承和Maven的生命周期
- POJ2431 优先队列+贪心