Remove Duplicates from Sorted Array II leetcode java
题目:
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的更多相关文章
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 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 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- 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 ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
随机推荐
- ARP监控工具ARPalert
ARP监控工具ARPalert ARP协议用于IP转化为Mac地址.由于ARP协议的缺陷,导致被用于中间人攻击.为了防止ARP攻击,Kali Linux提供了专用工具ARPalert.该工具可以对 ...
- BZOJ.2111.[ZJOI2010]排列计数(DP Lucas)
题目链接 对于\(a_i>a_{i/2}\),我们能想到小根堆.题意就是,求构成大小为\(n\)的小根堆有多少种方案. 考虑DP,\(f[i]\)表示构成大小为\(i\)的小根堆的方案数,那么如 ...
- Android任务和返回栈完全解析(转)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/41087993 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...
- 简表-Java-Echart报表介绍
Java后台报表尝试了很多,最终发现了一款,而且是开源的,简表地址:http://www.jatools.com/jor/.问题的引入:该报表支持嵌套,钻去,应对excel类似的报表,足够了.但是,报 ...
- CentOS启动OpenVPN报错:Failed to start OpenVPN Robust And Highly Flexible Tunneling Application On server.
tailf /var/log/openvpn.log 查看日志,里面有最详细的错误解说. 参考: https://forums.openvpn.net/viewtopic.php?t=21561
- Javascript Array和String的互转换。
Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...
- C#各种泛型集合体验
本篇体验除Queue<T>和Stack<T>之外的其它泛型集合. SortedList<TKey, TValue> SortedList<TKey, TVal ...
- ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定
本篇接着上一篇"ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API",尝试获取数据. 在Models文件夹下创 ...
- iOS sqlite 使用事务操作数据库
业务层代码: //将解析的更新人员数据批量同步到数据库 +(void)operateCompUsers:(NSMutableArray*)operateCompUsers { sqliteHelper ...
- Linux tar包相关命令
tar [-j|-z][cv][-f 新建的文件名] filename... <==打包与压缩 tar [-j|-z][tv][-f 新建的文件名] <==查看文件名 tar [-j| ...