[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 ...
随机推荐
- [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.
. 检索 dbo.sysobjects表, select count(*) from dbo.sysobjects where xtype='U' and Name = '你的表名' . 根据返回的结 ...
- 326.Power of Three
/* Given an integer, write a function to determine if it is a power of three. Follow up: Could you d ...
- (转)C#操作PPT
原文地址:http://blog.163.com/loveyingchun_1314/blog/static/2382425120124312627530/ 引用Microsoft.Office.Co ...
- js控制TR的显示隐藏
在很多现实的场景中,有的文本框我们希望在选择“是”的按钮之后才出现,这就需要js控制TR的隐藏和显示,如何控制,本文为大家揭晓 下文分享的一段代码:选择是的按钮就显示身高和体重的文本框的代码.注意:r ...
- 医失眠灵验方--五味子50g 茯神50g 合欢花15g 法半夏15g
方药:五味子50g 茯神50g 合欢花15g 法半夏15g 水煎服 主治:失眠健忘 此方为已故名老中医李培生之验方,用于临床治疗失眠健忘症,疗效显著,其主药为五味子,滋阴和阳,敛阳 ...
- android界面布局技巧(一)
(1)//得到手机的宽高 Display display = getWindowManager().getDefaultDisplay(); int screenWidth = display.get ...
- 测试方法---"SFDIPOT"
SFDIPOT方法是快速测试的一种方法,可以帮助你快速理清测试点. 我粗略地想了一下,凡是面试时我遇到过的奇葩的让我测一个电梯.雨伞.电话.水杯.测一个奇怪的东西的面试题上都能用. 然后嘛,日常工作中 ...
- 初探接口测试框架--python系列4
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 【Unity Shaders】学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert
[Unity Shaders]学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert 转载请注明出处:http://www.cnblogs.com/-867259 ...
- 将U盘分成 启动盘+文件存储区
我看了很多帖子,发现想要将U盘分区的朋友绝大部分是和我一样,想用U盘做成一个启动盘同时兼顾文件存储,分区的目的很简单,就是想将启动部分单独做成一个区,以免在日常的应用中使得启动文件染毒或者误操作造成损 ...