[LeetCode] Remove Duplicates from Sorted Array II [27]

题目

Follow up for "Remove Duplicates":

What if duplicates are allowed at most twice?

For example,

Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

原题链接(点我)

解题思路

移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次。

这个题类似Remove Duplicates from Sorted Array,第一个想法很直接就是计数,超过2次的就忽略,依据这个思路的代码见代码一;

上面的思路可行,但是代码看着比较冗余,判断比较多。再来想想原来的数组,该数组是排好序的,如果一个数出现3次以上,那么必有A[i] == A[i-2]。所以根据这个关系可以写出比较精简的代码二。详见代码。

代码实现

代码一
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if(A==NULL || n<=0) return 0;
        int start=1, count = 1, back = A[0];
        for(int i=1; i<n; ++i){
            if(count<2){
                A[start++] = A[i];
                if(back != A[i]){
                    back = A[i];
                    count = 1;
                }else{
                    ++count;
                }
            }else{
                if(A[i] != back){
                    count = 1;
                    A[start++] = A[i];
                    back = A[i];
                }else{
                    ++count;
                }
            }
        }
        return start;
    }
};
代码二
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if(A==NULL || n<=0) return 0;
        if(n==1) return 1;
        int start=1,back = A[1];
        for(int i=2; i<n; ++i){
            if(A[i] != A[i-2]){
                A[start++] = back;
                back = A[i];
            }
        }
        A[start++] = back;
        return start;
    }
};

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29822565
)

[LeetCode] Remove Duplicates from Sorted Array II [27],布布扣,bubuko.com

时间: 2024-09-29 05:56:27

[LeetCode] Remove Duplicates from Sorted Array II [27]的相关文章

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A =[1,1,1,2,2,3], Your function should return length =5, and A is now[1,1,2,2,3]. 题意:可以保留一个重复的元素. 思路:第一种是和Remove duplicates from sorte

[Leetcode] Remove Duplicates From Sorted Array II (C++)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. Tag: Array; Two Pointers 体会: 继续是quicksort中par

[LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中

LeetCode() Remove Duplicates from Sorted Array II

我的思路: 先判断是不是有两个相等的,如果有,从2个之后查找还有几个相同的,start计数. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0) return 0; int like=nums[0]; int flag=0; int start=0; for(int i=1;i<nums.size();++i) { if(like==nums[i]) { i

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte