标签:leetcode algorithm 数组 重复元素 移除
Given a sorted array, remove the duplicates in place such that each element appear only once 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]
.
移除数组中的重复元素,并返回新数组的长度。
这个题目应该算是简单的题目。使用两个变量就可以。具体的看代码
class Solution { public: int removeDuplicates(int A[], int n) { if(A==NULL || n<=0) return 0; int start = 1; int back = A[0]; for(int i=1;i<n;++i){ if(A[i] != back){ A[start++] = A[i]; back = A[i]; } } return start; } };
[LeetCode] Remove Duplicates from Sorted Array [19],布布扣,bubuko.com
[LeetCode] Remove Duplicates from Sorted Array [19]
标签:leetcode algorithm 数组 重复元素 移除
原文地址:http://blog.csdn.net/swagle/article/details/29211429