题目:
给定一个可包含重复数字的序列,返回所有不重复的全排列。
题目链接:Permutations II
C++:
多了个去重
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
int lens = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
per(res, nums, 0, lens - 1);
return res;
}
void per(vector<vector<int>> &r, vector<int> nums, int k, int len){
if(len == k){
r.push_back(nums);
return ;
}
for(int i = k; i <= len; i++){
if(i != k && nums[i] == nums[k])
continue;
swap(nums[i], nums[k]);
per(r, nums, k + 1, len);
}
}
};