码迷,mamicode.com
首页 > 编程语言 > 详细

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

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

标签:style   blog   http   color   io   ar   for   sp   div   

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-从有序数组移除重复-简单逻辑

标签:style   blog   http   color   io   ar   for   sp   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!