题目:

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

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

Your function should return length = 5, and A is now [1,1,2,2,3].

题解:

之前是不允许有重复的。

现在是可以最多允许2个一样的元素。

然后删除duplicate,返回长度。

删除duplicate的方法就是指针的操作。具体方法见代码。

代码如下:

 1     public int removeDuplicates(int[] A) {
 2         if (A.length <= 2)
 3             return A.length;
 4  
 5         int prev = 1; // point to previous
 6         int curr = 2; // point to current
 7  
 8         while (curr < A.length) {
 9             if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
                 curr++;
             } else {
                 prev++;
                 A[prev] = A[curr];
                 curr++;
             }
         }
  
         return prev + 1;
     }

Reference:http://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-array-ii-java/

Remove Duplicates from Sorted Array II leetcode java的更多相关文章

  1. Remove Duplicates from Sorted Array II [LeetCode]

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

  2. Remove Duplicates from Sorted Array II ——LeetCode

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

  3. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

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

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

  5. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

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

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

  8. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  9. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

随机推荐

  1. WEP自动破解工具wesside-ng

    WEP自动破解工具wesside-ng   wesside-ng是aircrack-ng套件提供的一个概念验证工具.该工具可以自动扫描无线网络,发现WEP加密的AP.然后,尝试关联该AP.关联成功后, ...

  2. ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge

    ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge Arduino环境与Linux环境的桥梁——Bridge 在第一章中介绍Arduino Yun硬件的时候提到过,它上面有 ...

  3. centos7 静默安装oracle

    系统centos7.4 mini 关闭selinux.firewalld 配置主机名: hostnamectl set-hostname  --static oracle 之前说oracle不认cen ...

  4. BZOJ.4909.[SDOI2017]龙与地下城(正态分布 中心极限定理 FFT Simpson积分)

    BZOJ 洛谷 https://www.luogu.org/blog/ShadowassIIXVIIIIV/solution-p3779# 正态分布 正态分布是随机变量\(X\)的一种概率分布形式.它 ...

  5. 7.30 正睿暑期集训营 A班训练赛

    目录 2018.7.30 正睿暑期集训营 A班训练赛 T1 A.蔡老板分果子(Hash) T2 B.蔡老板送外卖(并查集 最小生成树) T3 C.蔡老板学数学(DP NTT) 考试代码 T2 T3 2 ...

  6. Markdown基础用法

    1. 标题 文字前加#,共6级标题,# 一级标题,## 二级标题,...,###### 六级标题 2. 列表 文字前加-或* 即可变无序列表,文字前加 数字. 即可变有序列表 3. 引用 在引用文本前 ...

  7. Xtreme8.0 - Kabloom 动态规划

    Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...

  8. Mysql update case

    UPDATE table SET total = CASE WHEN total = '1' THEN total- 1 ELSE total = '2' END WHERE id = 17

  9. W3wp.exe占用CPU及内存资源

    问题背景 最近使用一款系统,但是经常出现卡顿或者用户账号登录不了系统.后来将问题定位在了服务器中的“w3wp.exe”这个进程.在我们的用户对系统进行查询.修改等操作后,该进程占用大量的CPU以及内存 ...

  10. poj2115

    构造出模线性方程c * x = b - a mod (2 ^ k) 很容易解. 利用LRJ书上的方法. #include <iostream> using namespace std; # ...