Remove Duplicates from Sorted Array II
来源:互联网 发布:天使与魔鬼 知乎 编辑:程序博客网 时间:2024/06/01 20:20
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]
.
Better Approach
Key to solve:
start from index: prev=1; curr=2 since allow twice duplication;
There are comparison cases:
1:Only twice duplication allow: compare with two prior elements:
2. Not accepted case.
Check coding in detail below
public class Solution { public int removeDuplicates(int[] A) { int len=A.length; if(len<3) return len; int prev=1; int cur=2; while(cur<len){ //accepted case if(A[cur]==A[prev] && A[cur]==A[prev-1]){ cur++; }else{ //unaccepted case prev++; A[prev]=A[cur]; cur++; } } return prev+1; }}
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- 在C和C++里const的用法异同总结
- ASP.NET MVC 中Ajax.ActionLink的用法
- MTK(3G)手机开发初学者必备资料(MMI)
- [译]Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
- Struts1 访问 action 指定的某一个方法
- Remove Duplicates from Sorted Array II
- <游戏改变世界>摘录
- C# sqliet 读写 *.db 数据库
- Linux中Framebuffer驱动详解1
- centos添加sudo用户
- openstack分析——NOVA中的RabbitMQ解析
- (总结)行内元素和块元素都有哪些
- sizeof 和 strlen
- HDU1232