思路
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<bool> st;
int n;
vector<vector<int>> permuteUnique(vector<int>& nums) {
n = nums.size();
st.resize(n);
path.resize(n);
sort(nums.begin(), nums.end());
dfs(nums, 0, 0); //人为规定顺序
return res;
}
void dfs(vector<int>& nums, int u, int start){
if(u == n){
res.push_back(path);
return;
}
for(int i = start; i < n; i++){
if(!st[i]){
st[i] = true;
path[i] = nums[u]; //i这个位置放哪个数
dfs(nums, u+1, u+1<n && nums[u+1]==nums[u]? i+1: 0);
st[i] = false;
}
}
}
};
网友评论