题目:
代码(首刷自解 2024年2月6日):
class Solution {
private:
vector<vector<int>> res;
vector<int> path;
public:
void backtracking(vector<int>& nums, int depth, vector<bool>& used) {
if (depth == nums.size()) {
res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (used[i] == true) continue;
path.push_back(nums[i]);
used[i] = true;
backtracking(nums, depth + 1, used);
used[i] = false;
path.pop_back();
}
return;
}
vector<vector<int>> permute(vector<int>& nums) {
res.clear();
path.clear();
vector<bool> used(nums.size(), 0);//记录同一树枝上已用过的节点
backtracking(nums, 0 ,used);
return res;
}
};