LeetCode: Remove Duplicates from Sorted Array II
来源:互联网 发布:韩国偶像团体 知乎 编辑:程序博客网 时间:2024/04/28 13:07
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) { int i = 0, j = 0, count = 0; while(j < n) { count = 0; while(j < n-1 && A[j] == A[j+1]) { j++; count++; } if(count >= 1) { A[i] = A[j]; i = i+1; } A[i] = A[j]; i++; j++; } return i; }};
Round 2:
class Solution {public: int removeDuplicates(int A[], int n) { int l = 0, r = 0, count = 0;while(r < n){if(r-1 >= 0 && A[r] == A[r-1]){count++;if(count <= 1){A[l++] = A[r++];}else{r++;}}else{count = 0;A[l++] = A[r++];}}return l; }};
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
- php接受android post请求报特殊异常
- 阿里云部署Docker(8)----安装和使用redmine
- bind1st和bind2nd函数用于将一个二元算子
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 介绍远程Event Receivers
- iOS开发的兼容和适配问题
- LeetCode: Remove Duplicates from Sorted Array II
- day56,page70
- 华为实验室
- jdk安装注意事项,特别是不同的目录区别
- JAVA API 操作HBASE(二)
- SSH原理之图文详解
- VC++调试技巧(一)
- Java基础——protected访问修饰符探讨
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期