标签:
题目链接https://leetcode.com/problems/remove-duplicates-from-sorted-array/
思路就是维护两个pointer,一个,i, 用于遍历整个数组,另一个,len, 用于保存当前的无重复元素的数组的最后一个position
if A[i] == A[len], A[i]重复,跳过,i++;
if A[i] != A[len], 把A[i]放到A[len+1],A[len + 1] = A[i], i++, len++;
class Solution { public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; int len = 0; for(int i = 1; i < n; i++) { if(A[i] != A[len]) { len++; A[len] = A[i]; } } return len + 1; } };
Reference:
http://bangbingsyb.blogspot.com/2014/11/leetcode-remove-duplicates-from-sorted.html
Remove Duplicates from Sorted Array
标签:
原文地址:http://www.cnblogs.com/walcottking/p/4430823.html