删除排序数组中的重复数字
利用库函数,偷懒法
class Solution {
public:
/**
* @param A: a list of integers
* @return : return an integer
*/
int removeDuplicates(vector<int> &nums) {
// write your code here
nums.erase(unique(nums.begin(), nums.end()), nums.end());
return nums.size();
}
};
class Solution {
public:
/**
* @param A: a list of integers
* @return : return an integer
*/
int removeDuplicates(vector<int> &nums) {
// write your code here
int len = nums.size();
if(len == 0) {
return 0;
}
int size = 1;
for(int i = 1; i < len; ++i) {
if(nums[i] == nums[i-1]) {
continue;
}
nums[size++] = nums[i];
}
return size;
}
};