80. Remove Duplicates from Sorted Array II

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.

 public static int removeDuplicatesMyself(int[] nums) {

        if (nums.length < 2) return nums.length;
        final int count = nums.length;
        int repeat = 2;
        int occor = 0;
        int index = 0;
        for (int i = 0; i < count; ++i) {
            if (occor < repeat) nums[index++] = nums[i];
            if (i < count - 1 && nums[i] == nums[i + 1]) {
                occor++;
            } else {
                occor = 0;
            }
        }
        return index;
    }

  修改repeat可以实现任意重复次数

 

leetcode 26 80 删除已排序数组中重复的数据的更多相关文章

  1. C++版 - 剑指offer面试题38:数字在已排序数组中出现的次数

    数字在已排序数组中出现的次数 提交网址: http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&t ...

  2. LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17

    442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n ...

  3. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  4. Java实现 LeetCode 442 数组中重复的数据

    442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...

  5. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  6. 【leetcode】153. 寻找旋转排序数组中的最小值

    题目链接:传送门 题目描述 现有一个有序数组,假设从某个数开始将它后面的数按顺序放到了数组前面.(即 [0,1,2,4,5,6,7] 可能变成 [4,5,6,7,0,1,2]). 请找出数组中的最小元 ...

  7. 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)

    出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...

  8. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

  9. leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. Apache Hadoop 2.0.2-alpha

    原文出处http://hadoop.apache.org/docs/r2.0.2-alpha/hadoop-yarn/hadoop-yarn-site/Federation.html HDFSF分为2 ...

  2. jasper(二):制作饼状图和柱状图

    在新建一个框架之后 我们也是要执行 add dataset,来添加一个链接数据库的语句,因为这是个饼状图,所以要用group by 全部放入右边的框架 点完成 接下来,就是要创建饼状图,就要点击 窗口 ...

  3. 3 sum closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. intersection of two linked lists.(两个链表交叉的地方)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. 怎么分别javascript写在<head>里还是<body>里面?

    怎么分别javascript写在<head>里还是<body>里面? 具体哪些语句写在<body>里,哪些语句写在<head>里 满意答案 BeginN ...

  6. ajax调用servlet

    1.利用myecilpse建立一个web项目 2.导入需要的包: commons-beanutils.jar commons-collections-3.1.jar       commons-lan ...

  7. Centos7 时区的设置

    Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置, 没准到了下一个版本, 该程序已经不见了. 比如 sysvinit ...

  8. Mybatis 系列6

    上篇系列5中 简单看了一下TypeHandler, 本次将结束对于mybatis的配置文件的学习, 本次涉及到剩下没提及到的几个节点的配置:objectFactory.databaseIdProvid ...

  9. Django1.8文档阅读手记

    主要集中在新特性上. Django1.8好像开始推荐使用python3 Django的QuerySet是延迟加载的,这个文档里面一般会明言,通过Connection SQL显示测试,外键对象也是延迟加 ...

  10. 在网页中使用particlesjs实现背景的动态粒子特效

    先上一张效果图: 这种动态的背景特效,制作起来其实非常简单. 使用了particles.js particles.js可以从github网站下载到最新的源码,网址是 https://github.co ...