首页 > 编程语言 > 详细

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

时间:2014-10-20 03:20:20      阅读:320      评论:0      收藏:0      [点我收藏+]

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;
    }
};

 

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

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

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