forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0046.cpp
More file actions
27 lines (25 loc) · 747 Bytes
/
0046.cpp
File metadata and controls
27 lines (25 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> path;
vector<bool> used(nums.size(), false);
dfs(nums, nums.size(), used, path, ans);
return ans;
}
private:
void dfs(vector<int>& nums, int target, vector<bool>& used, vector<int>& path, vector<vector<int>>& ans) {
if (target == 0) {
ans.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (used[i]) continue;
used[i] = true;
path.push_back(nums[i]);
dfs(nums, target - 1, used, path, ans);
path.pop_back();
used[i] = false;
}
}
};