题目

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. 00001 - Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别

    ①/etc/profile: 该文件登录操作系统时,为每个用户设置环境信息,当用户第一次登录时,该文件被执行.也就是说这个文件对每个shell都有效,用于获取系统的环境信息. # /etc/profi ...

  2. [SQL]SQL Prompt5的工具栏按钮介绍

    这是SQL Prompt5的工具栏按钮 Refresh Suggestions 刷新提示(数据库更改表结构后,需要刷新一下) Format SQL 格式化代码(Pro版本才能启用,可定制,所有前边带红 ...

  3. h5标签兼容

    <!--[if lt IE 9]> <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.js"> ...

  4. 在django中,执行原始sql语句

    extra()方法 结果集修改器,一种提供额外查询参数的机制 使用extra: 1:Book.objects.filter(publisher__name='广东人员出版社').extra(where ...

  5. 阿里云发送短信验证码php_SDK

    1.登录阿里云账号下载——aliyun-dysms-php-sdk(我使用的php版本) 下载地址:https://help.aliyun.com/document_detail/55359.html ...

  6. oracle常用加解密函数

    md5 CREATE OR REPLACE FUNCTION MD5( passwd IN VARCHAR2) RETURN VARCHAR2 IS retval varchar2(32); BEGI ...

  7. JVM总结-虚拟机加载类

    从 class 文件到内存中的类,按先后顺序需要经过加载.链接以及初始化三大步骤.其中,链接过程中同样需要验证:而内存中的类没有经过初始化,同样不能使用.那么,是否所有的 Java 类都需要经过这几步 ...

  8. java.lang.ClassNotFoundException: org.thymeleaf.spring5.view.ThymeleafViewRe。。。。。。。。。。。

    直接上代码问题: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoc ...

  9. python中赋值-浅拷贝-深拷贝之间的关系

    赋值: 变量的引用,没有拷贝空间 对象之间赋值本质上 是对象之间的引用传递而已.也就是多个对象指向同一个数据空间. 拷贝的对象分两种类型: . 拷贝可变类型 浅拷贝: 只拷贝第一层数据,不关心里面的第 ...

  10. [Android]Android布局优化之<include />

    转载请标明:转载于http://www.cnblogs.com/Liuyt-61/p/6602891.html -------------------------------------------- ...