题目:

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. HDU 5813 Elegant Construction 构造

    Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  2. logstash grok 分割匹配日志

    使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...

  3. LPC43XX TQFP144 IO Pinout

  4. POJ 3076 SUKODU [Dangcing Links DLX精准覆盖]

    和3074仅仅有数目的不同,3074是9×9.本来想直接用3074的.然后MLE,,,就差那么20M的空间,,. 从这里学习到了解法: http://www.cnblogs.com/ylfdrib/a ...

  5. 安装oracle环境变量path的值大于1023的解决办法

    介绍解决安装oracle安装问题 方法/步骤     安装oracle 10g时遇到环境变量path的值超过1023字符,无法设置该值,如图: ‍ 安装oracle 10g时遇到环境变量path的值超 ...

  6. Running Jenkins behind Nginx

    original : https://wiki.jenkins-ci.org/display/JENKINS/Running+Jenkins+behind+Nginx In situations wh ...

  7. 将 tomcat 安装成 windows 服务

    1.下载 tomcat 的windows 压缩包,一般以 .zip ,而且文件名中有 bin 的文件就是 2.解压下载的文件到某一个目录下,eg: TOMCAT_HOME 3.打开 cmd ,运行 % ...

  8. DotNetty 学习

    [转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...

  9. Ioc:autofac lifetime scope.

    During application execution, you’ll need to make use of the components you registered. You do this ...

  10. python测试开发django-28.发送邮件send_mail

    前言 django发邮件的功能很简单,只需简单的配置即可,发邮件的代码里面已经封装好了,调用send_mail()函数就可以了 实现多个邮件发送可以用send_mass_mail()函数 send_m ...