这题跟Permutaion差不多,图省事用next_permuatation()做的,只是要加一个set。
解法1:
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int> > vv;
vector<int> orig(nums);
if (nums.size()==0) return vv;
set<vector<int> > s;
do {
s.insert(nums);
next_permutation(nums.begin(), nums.end());
} while(nums!=orig);
for (auto it=s.begin(); it!=s.end(); ++it) {
vv.push_back(*it);
}
return vv;
}
int main()
{
vector<int> nums={1,1,2};
vector<vector<int> > vv=permuteUnique(nums);
cout<<"["<<endl;
for (auto i : vv) {
cout<<" [";
for (auto j : i) cout<<j<<" ";
cout<<"],"<<endl;
}
cout<<"]"<<endl;
return 0;
}
解法2:回溯?