leetcode47. Permutations II
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result = {};
vector<vector<int>> retResult = {};
vector<int> temp = {};
if (nums.size()<1) {
result.push_back(temp);
return result;
}
if (nums.size() == 1) {
result.push_back(nums);
return result;
}
if (nums.size() == 2) {
result.push_back(nums);
if(nums[1]!=nums[0]){
temp.push_back(nums[1]);
temp.push_back(nums[0]);
result.push_back(temp);
}
return result;
}
sort(nums.begin(),nums.end());
temp.assign(nums.begin(), nums.end());
/*
for (int i = 0; i<temp.size(); i++) {
cout << temp[i];
}*/
for (int j = 0; j < nums.size(); j++) {
temp.erase(temp.begin()+j);
result = permuteUnique(temp);
for (int i = 0; i<result.size(); i++) {
result[i].insert(result[i].begin(),nums[j] );
retResult.push_back(result[i]);
}
while(j+1<nums.size() && nums[j] == nums[j+1]) j++;
temp.assign(nums.begin(), nums.end());
}
return retResult;
}
};