LeetCode 26 Remove Duplicates from Sorted Array
#include <vector>
#include <algorithm>//unique函数,stl
using namespace std;
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
nums.erase(unique(nums.begin(), nums.end()), nums.end());
return nums.size();
}
};//AC, 49ms,很慢。肯定还有快的方法
class Solution3{
public:
int removeDuplicates(vector<int>&nums){
if (nums.size() == 0)
return 0;
int i = 1;
while(i !=nums.size()){
//if (i + 1 == nums.size())
//break;
if (nums[i] == nums[i - 1]){
nums.erase(nums.begin() + i);
}
else
i++;
}
return nums.size();
}
};//还是特别慢……