//题目
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
//给定一个从小到大排序的数组,将其中所有出现次数>1的元素去除,返回最终的元素个数
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
//使用两个循环不重复的遍历,但是否有重复的元素,有就删除一个
long length = nums.size();
for(int i = 0; i < nums.size(); i++)
for(int j = i+1; j < nums.size(); j++){
if(nums[i]==nums[j]){
nums.erase(nums.begin()+j);
j=j-1; //重复元素删除后,下标改变,但是vector里面不好移动,所以向左移动j来防止漏掉某些元素
}
}
long n = nums.size();
return n;
}
};
int main(int argc, const char * argv[]) {
Solution s1;
vector<int> nums;
nums.push_back(1);
nums.push_back(1);
nums.push_back(1);
nums.push_back(1);
nums.push_back(5);
cout<<s1.removeDuplicates(nums);
return 0;
}