LeetCode:Remove Duplicates from Sorted Array I&&II
2013-07-30 15:37
211 查看
bug处
我在处理时候从数组最后开始处理,则前面数据遭到了污染。
一定得考虑数据污染,如果你从头开始存,则每次比较得与后面比较,切勿再与前面比较
我在处理时候从数组最后开始处理,则前面数据遭到了污染。
class Solution { public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n==0) return 0; int length = 1; for(int i = 1;i<n;i++) if(A[i]!=A[i-1]) length++; int p=0; for(int i = 0;i<length;i++) { int val = A[p]; A[i] = val; while(A[p]==val) { p++; } } return length; } };
一定得考虑数据污染,如果你从头开始存,则每次比较得与后面比较,切勿再与前面比较
class Solution { public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<=2) return n; int length = 2; for(int i =2;i<n;i++) if(A[i]!=A[i-1]||A[i]!=A[i-2]) length++; int p = 0; for(int i =0;i<length;i++) { while(p+2<n&&A[p]==A[p+1]&&A[p]==A[p+2]) { p++; } A[i]= A[p]; p++; } return length; } };
相关文章推荐
- [Leetcode] #26#80 Remove Duplicates from Sorted Array I & II
- Leetcode | Remove Duplicates from Sorted Array I && II
- LeetCode - Remove Duplicates from Sorted Array I && II
- leetcode-26&80 Remove Duplicates from Sorted Array I & II
- [LeetCode]26. Remove Duplicates from Sorted Array I&II
- 【LeetCode】Remove Duplicates from Sorted Array I && II
- leetcode-26&80 Remove Duplicates from Sorted Array I&II
- 【LeetCode从零单刷】Remove Duplicates from Sorted Array I & II
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
- leetcode - Remove Duplicates from Sorted Array I && II
- leetcode之Remove Duplicates from Sorted Array II
- [leetcode题后感] Remove Duplicates from Sorted Array I、 II
- 【LeetCode】-Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array I&&II——怎样防超时
- Remove Duplicates from Sorted Array II -- leetcode
- Leetcode_80_Remove Duplicates from Sorted Array II
- 【leetcode】Array——Remove Duplicates from Sorted Array II(80)
- LeetCode:Remove Duplicates from Sorted Array II
- [面试真题] LeetCode:Remove Duplicates from Sorted List I & II
- LeetCode-80-Remove Duplicates from Sorted Array II(递归)-Medium