您的位置:首页 > 其它

Remove Duplicates from Sorted Array II 分类: Leetcode(线性表) 2015-03-25 21:20 40人阅读 评论(0) 收藏

2015-03-25 21:20 489 查看


Remove Duplicates from Sorted Array II



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==0) return 0;
int idx = 0, count = 0 , i = 0;
for ( i = 0; i < n ; i++) {
if (i >0 && a[i] ==a[i-1]) {
count++;
if(count >= 3){
continue;
}
}
else {
count = 1;
}
a[idx++] = a[i];
}
return idx;
}
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐