题目:一个有序数组,要求保证数组中的每个元素不能超过2个。

    输入:A = [1,1,1,2,2,3]  输出:length = 5, and A is now [1,1,2,2,3]

思路:双指针 。有些绕,不过理清了后,思路还是很直接明朗的。

1、两个指针:p和help。初始化时同时指向数组头。变量cur记录当前元素值,count记录当前元素值出现的次数。

2、当 A[p] == cur && count < 2 或者 A[p] != cur 的时候,A[help] = A[p], help++ , p++。

3、当 A[p] == cur && count >= 2时,p++,help不动。

4、直到p指向尾部,此时help的值即为去重后的数组长度。

代码:

 public int removeDuplicates(int[] A) {
if(A.length == 0) return 0; int cur = A[0] , count = 1;
int help = 1;
for(int p = 1 ; p < A.length ; p++){
if(cur == A[p]){
if(count < 2){
count++;
A[help++] = A[p];
}
}else{
count = 1;
cur = A[p];
A[help++] = A[p];
}
}
return help;
}

[leetcode]_Remove Duplicates from Sorted Array II的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  2. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

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

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

  4. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  5. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  6. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

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

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

  9. 【leetcode】Remove Duplicates from Sorted Array II

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

随机推荐

  1. Microsoft visual studio中文字样输出

    解决办法: 可以尝试下通过: 1.file->高级保存选项-> 2.工具->选项->文本编辑器->自动检测不带签名的UTF-8编码

  2. 获取当前访问的url

    1.获取完全url,包含参数: request.getRequestURL(); 2.获取部分: request,getRequestURI 不包含参数,协议名称 获取访问的参数: request.g ...

  3. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  4. cocos2dx 锁定30帧设置

    修改代码: AppDelegate.cpp // set FPS. the default value is 1.0/60 if you don't call this pDirector->s ...

  5. Nginx作为简单代理服务器(Windows环境)

    Nginx一个频繁的应用是作为代理服务器,由Nginx代理服务器接受客户请求,并将客户请求发送到应用服务器处理,接受应用服务器的响应,然后将响应发送给客户. 现在要做的一个应用场景就是当客户请求图片资 ...

  6. 关于java.lang.IllegalStateException

    今天调试程序时遇到了java.lang.IllegalStateException org.apache.catalina.connector.ResponseFacade.sendRedirect( ...

  7. 二叉树删除 lisp

    ;;; From ANSI Common Lisp ; If you have questions or comments about this code, or you want; somethin ...

  8. 实时显示GetLastError值

    在Watch窗口中输入$err,hr 自动显示上个函数返回值

  9. ASP.NET MVC4 学习系统五(Razor)

    Razor ,你好!       Razor 是一种把代码和内容进行平滑集成的语法.尽管它引入了一些新的符号和关键字,但是Razor并不是一种新的语法.相反,Razor允许用户使用已知的语言来编写代码 ...

  10. CLRS: online maximum (n,k)algorithm

    //the first k elements interviewed and rejected, //for  the latter n-k elements ,if value >max,re ...