[leetcode]_Remove Duplicates from Sorted Array II
题目:一个有序数组,要求保证数组中的每个元素不能超过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的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- Nginx 配置指令location 匹配符优先级和安全问题【转】
Nginx配置指令location匹配符优先级和安全问题 使用nginx 很久了,它的性能高,稳定性表现也很好,得到了很多人的认可.特别是它的配置,有点像写程序一样,每行命令结尾一个";&q ...
- [ActionScript 3.0] AS3调用百度地图API
package { import baidu.map.basetype.LngLat; import baidu.map.basetype.Size; import baidu.map.config. ...
- affine transformation matrix 仿射变换矩阵 与 OpenGL
变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可采用的变换模型有如下几种:刚性变换.仿射变换.透视变换和非线形变换等,如下图: 参考: ht ...
- POJ - 1159 Palindrome(dp-回文变形)
d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...
- [SQL] 不知道是什么存储过程
CREATE PROCEDURE dt_DXS_STAFF_ACTIVE @STAFFSTATUS INT, @STAFFNUM INT, @STARTNO INT, @@TOTALCOUNT INT ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
- Hive基础之HiveServer2 JDBC的使用
启动HiveServer2: cd $HIVE_HOME/bin 以后台方式默认端口启动HiveServer2(默认端口是10000):hiveserver2 & 以后台方式指定端口的方式启动 ...
- JavaScript对象的创建之外部属性定义方式(基于已有对象扩充其属性和方法)
var person = new Object(); person.name = "luogk"; person.age = 33; person.say = function() ...
- WinCE下使用C#获得带毫秒的DateTime.Now
在WinCE下,使用DateTime.Now获取的系统时间是不带毫秒的,如果想要它带毫秒,需要耍点手段.话不多说,直接上代码: public static DateTimePrecisely { // ...
- Jmeter如何设置断言
1.打开飞机订票网站,登录后,确定要查找的关键字为reservation :2.在录制的登录脚本里,添加察看结果树监听器:3.运行一遍脚本:4.在察看结果树里查找reservation文本,并定位页面 ...