思路:直接调用STL库函数 (简单但是肯定不会快)
Vector中移除重复元素
#include <algorithm>
unique(nums.begin(), nums.end());//重复元素甩最后,返回的是最后一个非重复元素的后一个地址
但是要记住:要先排序才能用
sort(nums.begin(), nums.end());
unique(nums.begin(), nums.end())
size=unique(nums.begin(), nums.end()) - nums.begin();
# include<iostream>
# include<vector>
# include<string>
# include<algorithm>
# include<math.h>
# include<climits>
# include<stack>
# include<queue>
using namespace std;
int removeDuplicates(vector<int>& nums) {
sort(nums.begin(), nums.end());//快排
int size = unique(nums.begin(), nums.end())-nums.begin();//唯一函数
return size;
}
int main(void) {
vector<int> nums;
nums.push_back(1);
nums.push_back(1);
nums.push_back(4);
cout << removeDuplicates(nums) << endl;
system("pause");
return 0;
}