题目链接:https://leetcode-cn.com/problems/permutations-ii/
题目如下:
class Solution {
public:
vector<int> path;
vector<vector<int>> result;
vector<vector<int>> permuteUnique(vector<int>& nums) {
result.clear();
path.clear();
sort(nums.begin(),nums.end());//如果要去重,一定要先排序
vector<bool> used(nums.size(),false);
backtracking(nums,used);
return result;
}
void backtracking(vector<int>& nums,vector<bool>& used){
if(path.size()==nums.size()){
result.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
if(i>0&&nums[i]==nums[i-1]&&used[i-1]==true) continue;//说明在同一树枝nums[i-1]使用过
if(used[i]==false){
used[i]=true;
path.push_back(nums[i]);
backtracking(nums,used);
path.pop_back();
used[i]=false;
}
}
}
};