您的位置:首页 > 其它

[LeetCode] 081: Remove Duplicates from Sorted Array II

2017-09-10 20:51 381 查看
[Problem]

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]
.

[Solution]
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= 2)return n;
int i = 1, j = 2;
while(j < n){
if(!(A[i] == A[j] && A[i-1] == A[j])){
A[++i] = A[j];
}
j++;
}
return i+1;
}
};
说明:版权所有,转载请注明出处。Coder007的博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: