[Leetcode] Remove Duplicates from Sorted Array II
来源:互联网 发布:阿里云计算机有限公司 编辑:程序博客网 时间:2024/06/03 22:38
题目:
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]
.
思路:双指针,设置一个flag,可以重复一次。
class Solution {public: int removeDuplicates(int A[], int n) { if (n <= 0) return n; int runner = 0; bool allow_dup = true; for (int i = 1; i < n; ++i) { if (A[i] == A[runner]) { if (allow_dup) { A[++runner] = A[i]; allow_dup = false; } } else { allow_dup = true; A[++runner] = A[i]; } } return runner + 1; }};
总结:复杂度为O(n).
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
- UIImageView userInteractionEnabled
- Android的px、dp和sp
- 【诚邀各位开发者参加】2014-10-25 深圳OGEngine手游开发者交流会议
- 命令行执行java程序
- find命令【linux】
- [Leetcode] Remove Duplicates from Sorted Array II
- 程序员需要人脉
- 为何需要静音检测
- php关于引用的解释
- Qt QDialog窗体显示到屏幕中央
- 反弹端口 HTTP代理 HTTP隧道技术
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
- UITextField计算中英文混合的字符长度,中文2字节,英文1字节
- Spring源码由浅入深系列三 refresh