Remove Duplicates from Sorted Array II -- Leetcode
来源:互联网 发布:2016qq钓鱼网站源码 编辑:程序博客网 时间:2024/06/10 19:49
Remove Duplicates from Sorted Array II
12.21 2014
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]
.
class Solution {public: int removeDuplicates(int A[], int n) { if(n<=2) return n; int index=2; for(int i=2;i<n;i++){ if(A[index-2]!=A[i]) A[index++]=A[i]; } return index; }};
1. 当长度不超过2时,无论如何都会符合条件要求
2. 应从第三个element (i=2) 开始进行比较,index也应该从2开始
3. 应该先覆盖了以后再进行index的更新,因为index本身就是从需要被覆盖的位置开始
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Leetcode Remove Duplicates from Sorted Array II
- 《4399、小米、UC、百度面经》
- 修复华硕笔记本fn+f2在ubuntu下wifi不能够正常使用和WiFi Disabled (Hard-blocked)
- 使用javassist动态注入代码
- android获取设备屏幕分辨率的方法
- 百度地图定位
- Remove Duplicates from Sorted Array II -- Leetcode
- exec sp @vari output 和 exec @vari=sp @vari2 output 区别
- rhel5配置centos yum源
- WebOffice使用一
- 《2013年10月15号360一面》
- 前端架构那些事儿
- linux c mysql的一些细节
- ubuntu14.04安装搜狗输入法
- IE11 不支持 document.selection