[LeetCode] Remove Duplicates from Sorted Array II
2014-06-16 09:46
246 查看
题目:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A =
Your function should return length =
解答:
思路:相比“Remove Duplicates from Sorted Array”,在遍历的过程中加了一个计数器,保证最多重复次数为2.
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 <= 1) { return n; } int num = 1; int cur = 0; for(int i = 1; i < n; i++) { if(A[i] == A[cur]) { if(num < 2) { A[++cur] = A[i]; num++; } else { continue; } } else { num = 1; A[++cur] = A[i]; } } return cur + 1; } };
思路:相比“Remove Duplicates from Sorted Array”,在遍历的过程中加了一个计数器,保证最多重复次数为2.
相关文章推荐
- 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
- LeetCode题解:Remove Duplicates from Sorted Array I and II
- [LeetCode]26. Remove Duplicates from Sorted Array I&II
- 【LeetCode with Python】 Remove Duplicates from Sorted Array II
- LeetCode刷题(C++)——Remove Duplicates from Sorted Array II(Medium)
- leetcode:Remove Duplicates from Sorted Array II
- 【Leetcode】Remove Duplicates from Sorted Array II
- [leetcode] python Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II - LeetCode 80
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- LeetCode Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【LEETCODE】80-Remove Duplicates from Sorted Array II [Python]
- leetcode:Remove Duplicates from Sorted Array II 菜鸟解法
- Leetcode--Remove Duplicates from Sorted Array II
- LeetCode--No.80--Remove Duplicates from Sorted Array II