一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个数组,删除里面重复两次以上的数字,可以参考我的这篇博文【一天一道LeetCode】#26. Remove Duplicates from Sorted Array

解题思路:用一个计数器纪录数字出现的次数啊,超过两次及其以上都删除。

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int count = 0 ;//计数器
        int retlen = nums.size();
        if(retlen<=1) return retlen;
        for(auto iter = nums.begin() +1; iter != nums.end() ; )
        {
            if(*iter == *(iter-1)){//与上一个数字相等
                count++;//计数加1
                if(count>=2)//如果大于等于2,表示这个数需要删除
                {
                    iter = nums.erase(iter);//擦除!注意这里迭代器需要指向erase的返回值
                    retlen--;//长度减1
                }
                else ++iter;
            }
            else
            {
                count = 0;//如果与上一个数字不同就要重置计数器
                ++iter;
            }
        }
        return retlen;
    }
};

【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II的更多相关文章

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

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

  2. [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% ...

  3. [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 ...

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

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

  5. [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 ...

  6. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  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/#/description 给定一个已经排好序的数组 ...

  9. Leetcode#80 Remove Duplicates from Sorted Array II

    原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...

  10. [LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值

    和第一题不同的地方是,容忍两次重复 虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的 没说清楚题意 自己是用双指针做的,看了大神的答案更简单 public int removeDu ...

随机推荐

  1. PHP 5 Array 函数

    PHP Array 简介 PHP Array 函数允许您访问并操作数组. 支持简单的数组和多维数组. 安装 PHP Array 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP 5 ...

  2. linux下安装apache(httpd-2.4.3版本)各种坑

    博主的linux是ubuntu 14.04.3. 在安装apache最新版httpd-2.4.3的时候遇到各种坑. 先提供安装apache httpd-2.4.3所需要的包,博主已经整理好,下载地址: ...

  3. move_uploaded_file的failed to open stream错误处理

    PHP的基本语法学习的差不多了,现在开始学习PHP的文件上传功能实现了.功能中使用到了move_uploaded_file方法,运行时报错: failed to open stream. 经过查资料, ...

  4. 剑指Offer——关于劳动合同,这6件事毕业生必须知道!

    剑指Offer--关于劳动合同,这6件事毕业生必须知道!   求职找工作,不少人拿到劳动合同的那刻,可能连合同内容都没看清,就挥着笔杆子"签签签".别急!劳动合同包含哪些条款你清楚 ...

  5. CSS3 滤镜学习

    html篇 样式篇 grayscale sepia saturate hue-rotate invert opactiy brightness contrast blur drop-shadow 综合 ...

  6. Redis中的关系查询

    本文对Redis如何保存关系型数据,以及如何对其匹配.范围.模糊查询进行举例讲解,其中模糊查询功能基于最新的2.8.9以后版本. 1 关系型数据的存储 以Staff对象为例,在关系型数据库或类似Gri ...

  7. 微信小程序基础之创建使用教程

    本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志. 1. 获取 ...

  8. shell test和find命令实例解析

    shell test和find命令实例解析 下面以\build\core\product.mk相关部分来学习 define _find-android-products-files $(shell t ...

  9. Java提升篇之反射的原理(二)

    Java提升篇之通过反射越过泛型检查 /* *问题:在一个ArrayList<Integer>对象中,在这个集合中添加一个字符串. */ 在我们还没有学反射前,遇到这个问题都是无法实现的, ...

  10. 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码

    原文出处: 平凡之路的博客   欢迎分享原创到伯乐头条 伯乐在线注:『阅读优秀代码是提高开发人员修为的一种捷径』http://t.cn/S4RGEz .之前@伯乐头条 曾发过一条微博:『C 语言进阶有 ...