LeetCode-Remove Duplicates from Sorted Array-从有序数组移除重复-简单逻辑

时间:2014-10-20 03:20:20   收藏:0   阅读:321

https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

用一个cnt记录不重复的部分,后面每遇到不重复的cnt++即可。

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if (n==0) return 0;
        int last=A[0]-1;
        int count=1;
        for(int i=0;i<n;i++){
            if(A[count-1]!=A[i]) {
                A[count++]=A[i];
            }
        }
        return count;
    }
};

 

原文:http://www.cnblogs.com/yangsc/p/4036348.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!