[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 server中的锁 事务锁 更新锁 保持锁 共享锁 你知道吗?
锁定数据库的一个表 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其 ...
- 标准化命名CSS类,持续更新
放链接.持续化更新,以后可能会用上.https://github.com/zhangxinxu/zxx.lib.css/blob/master/zxx.lib.css
- photoshop CS 调整选择区域的大小
网上看到说:矩形选框不能直接调整大小,如果你不想重新画一个可以利用转换路径,然后再调整.这是不对的,矩形选框是可以调整大小的,使用"变换选区"即可. 对应步骤截图如下: 1.画 ...
- [Excel]C#操作Excel(导入导出)
/// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...
- [POJ 1635] Subway tree systems (树哈希)
题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...
- [C# 基础知识系列]C#中易混淆的知识点
一.引言 今天在论坛中看到一位朋友提出这样的一个问题,问题大致(问题的链接为:http://social.msdn.microsoft.com/Forums/zh-CN/52e6c11f-ad28-4 ...
- 性能测试脚本新玩法---fiddler&&Jmeter
飞测说:最近接触移动项目,测试app,需要做移动app的性能测试,想通过代理来录制,但是jmeter的代理录制效果真心不是很好,自己一个个请求来写代码,太浪时间了,那么有没有其他的办法呢? 我们都知道 ...
- Flex 使用列表和表格
Flex 设计了不同的控件来实现列表和表格,不仅能够将数据显示在表格和列表中,还可以实现对数据进行操纵,修改等更加强大的功能. 与列表和表格相关的控件如下所示: 列表控件(List Control): ...
- TCP/IP详解学习笔记(2)-- 数据链路层
1.概述 数据链路层属于计算机网络的底层,使用的信道主要有点对点信道和广播信道两种类型. 在TCP/IP协议族中,数据链路层主要有三个目的: 1)为IP模块发送 ...
- struts2项目需要加入的jar包
asm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcommons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommon ...