2019独角兽企业重金招聘Python工程师标准>>>

问题:

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 1122 and 3. It doesn't matter what you leave beyond the new length.

解决:

① 只需要维护一个count,当count是2时,就直接跳过即可,否则说明元素出现次数没有超,继续放入结果数组,若遇到新元素则重置counter。

class Solution {//1ms
    public int removeDuplicates(int[] nums) {
        if(nums.length <= 2) return nums.length;
        int index = 0;
        int count = 0;
        for (int i = 0;i < nums.length;i ++) {
            if(i >0 && nums[i] == nums[i - 1]){
                count ++;
                if(count >= 3){
                    continue;
                }
            }else{
                count = 1;
            }
            nums[index ++] = nums[i];
        }
        return index;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1538629

删除重复值(2以上)Remove Duplicates from Sorted Array II相关推荐

  1. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

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

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

  3. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  4. 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II

    重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...

  5. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II

    题目: 删除排序数组中的重复数字 II 跟进"删除重复数字": 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A ...

  6. Remove Duplicates from Sorted Array II leetcode java

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

  7. [LeetCode] Remove Duplicates from Sorted Array II

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

  8. LeetCode之Remove Duplicates from Sorted Array II

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

  9. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

最新文章

  1. Python生物信息学①将RNA序列翻译成蛋白质序列。
  2. 美国微生物科学院22年院士公布!舒跃龙、黄力、卢洪洲、赵国屏!
  3. 零代码如何打造自己的实时监控预警系统
  4. HealthKit开发快速入门教程之HealthKit框架体系创建健康AppID
  5. Spring集成TestNg测试
  6. 约瑟夫问题pascal程序
  7. 深入理解Spark 2.1 Core (十四):securityManager 类源码分析
  8. CAE所表示的计算机术语是,计算机应用中,英文缩略语CAE所表示的计算机术语是()。...
  9. 为什么有些女孩在发现渣男的真面目以后,还喜欢他们?
  10. 资源地址整合与备份,即时更新
  11. 利用python进行数据分析——第十四章_数据分析案例
  12. 【转】ThinkPHP命令行工具Tptool2.0使用教程
  13. 学习:大文件统计与排序
  14. SadpTool 海康设备网络搜索工具
  15. Tracepro中up vector和normal vector的定义2
  16. php 根据身份证计算年龄
  17. 阿里巴巴线上面试总结
  18. java健身房管理系统业务_基于SSM的健身房管理系统
  19. Centos7防火墙与IPTABLES详解
  20. 初级测试工程师面试指南

热门文章

  1. 20P49 Premiere制作信号损坏故障动画节奏运动风格宣传视频开场片头下载
  2. 多线程练习:铁路售票(线程安全)
  3. OR-Tools求解仓库选址和钢材取料问题
  4. oracle中给数据排序号,oracle中对排序的小结
  5. [亲测]Oracle查询--单表查询,连接查询(一)
  6. 用户体验之忘记密码之后
  7. python自动登录教程_python+selenium实现163邮箱自动登陆的方法
  8. 黑白照片上色软件app有哪些?这几款软件简单易上手
  9. crontab安装和用法(定时任务)
  10. java如何手撕加载字节码的代码?编写一个加载class文件的方法