您的位置:首页 > 其它

Leetcode Remove Duplicates from Sorted Array

2013-10-15 22:22 288 查看
数组删除重复元素,要求常数的空间复杂度。比较简单。

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 == 0 || n == 1)    return n;
int tmp = A[0], pos = 1, cnt = n;
for(int i = 1; i < n; i++){
if(tmp == A[i]){
cnt--;
}
else{
A[pos] = tmp = A[i];
pos++;
}
}
return cnt;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息