题目

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.

解答

这题又是一遍AC。。。只能说LeetCode上水题有点多,这居然都是Medium难度。。。

拿一个变量计数一下重复次数就可以了,每次遇到新的数就将这个变量赋值为1,如果这个变量达到2且遇到和之前重复的数,就移除。

下面是AC的代码:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() == 0){
            return 0;
        }
        int last = nums[0];
        int count = 1;
        int dup = 1;
        for(vector<int>::iterator iter = nums.begin() + 1; iter != nums.end(); iter++){
            if(*iter == last){
                if(dup < 2){
                    dup++;
                    count++;
                }
                else{
                    nums.erase(iter);
                    iter--;
                }
            }
            else{
                dup = 1;
                count++;
                last = *iter;
            }
        }
        return count;
    }
};

118

LeetCode OJ 80. Remove Duplicates from Sorted Array II的更多相关文章

  1. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  5. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

随机推荐

  1. AnimationDrawable写贞动画,播放完毕停止在第一或最后一帧

    animation.stop();animation.selectDrawable(0);//只需要在停止的时候,设置下标为你想要的一帧就好了

  2. Centos7下安装Python3.7.2

    在我的Centos7中,Python默认是安装的,输入python 直接可以查看版本号,入下图 注意:如果本机安装了python2,尽量不要管它,使用python3运行python脚本就好,因为可能有 ...

  3. 使用fakeroot模拟root权限执行程序(转)

    Hack #57: 使用fakeroot模拟root权限执行程序 fakeroot是什么 例如Debian在生成package的时候,编译完之后,不能立刻在当前环境执行make install,需要执 ...

  4. 程序集生成失败 -- 引用的程序集“ThoughtWorks.QRCode”没有强名称,为没有源码的程序集强签名

    如果你写的程序程序集是带签名的,应用了没有签名的程序集,编译就会报下面的错误 引用的程序集“**”没有强名称 进入sdk提示符界面,依次输入如下指令 sn -k ThoughtWorks.QRCode ...

  5. 第10章 网络安全(5)_访问控制列表ACL

    6. 访问控制列表ACL 6.1 标准访问控制列表 (1)标准ACL ①标准ACL是基于IP数据包的源IP地址作为转发或是拒绝的条件.即,所有的条件都是基于源IP地址的. ②基本不允许或拒绝整个协议组 ...

  6. javascript-关于赋值的那点事

    var ary1=[3,4]; var ary2=ary1; ary2[0]=1; ary2=[6,5] console.log(ary1) console.log(ary2) 个人测试出的结果是:更 ...

  7. Spark Streaming实时数据分析

    [kfk@bigdata-pro01 softwares]$ sudo rpm -ivh nc-.el6.x86_64.rpm Preparing... ####################### ...

  8. 运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist

    第一步:在管理员命令中输入: mysql_upgrade -u root -p --force 第二步:关闭并重启数据库 service mysql stop service mysql start

  9. html中header,footer分别固定在顶部和底部

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>page01</title> 5 <styl ...

  10. 【转】AD常用端口

    通常在域环境中我们有部分设备在DMZ区时,就需要知道AD的相关端口.在此提供给各位: 用户登录与验证身份时会用到的连接端口用户登录时会用到以下的服务,因此如果用户的计算机与域控制器之间被防火墙隔开,就 ...