首页 > 其他 > 详细

Remove Duplicates from Sorted Array

时间:2015-04-16 06:39:56      阅读:239      评论:0      收藏:0      [点我收藏+]

题目链接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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!