AcWing 51. 数字排列
原题链接
中等
作者:
Value
,
2020-09-03 19:04:10
,
所有人可见
,
阅读 367
class Solution {
public:
vector<int> part;
vector<vector<int>> res;
vector<vector<int>> permutation(vector<int>& nums) {
part.resize(nums.size());
sort(nums.begin(), nums.end());
dfs(nums, 0, 0, 0);
return res;
}
void dfs(vector<int>& nums, int u, int start, int state){
if(u == nums.size()){
res.push_back(part);
return ;
}
if(!u || nums[u] != nums[u - 1]) start = 0;
for(int i = start; i < nums.size(); i ++ ){
if(!(state >> i & 1)){
part[i] = nums[u];
dfs(nums, u + 1, i + 1, state | (1 << i));
}
}
}
};