AcWing 51. 数字排列
原题链接
简单
作者:
SayYong
,
2024-10-23 08:35:23
,
所有人可见
,
阅读 2
class Solution {
public:
vector<int> path;
vector<vector<int>> result;
void dfs(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] == false) continue;
if (!used[i]) {
used[i] = true;
path.push_back(nums[i]);
dfs(nums, used);
path.pop_back();
used[i] = false;
}
}
}
vector<vector<int>> permutation(vector<int>& nums) {
path.clear();
result.clear();
vector<bool> used(nums.size(), false);
sort(nums.begin(), nums.end());
dfs(nums, used);
return result;
}
};