-
Notifications
You must be signed in to change notification settings - Fork 3
/
46.cpp
26 lines (26 loc) · 798 Bytes
/
46.cpp
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
class Solution {
public:
vector<vector<int>> res;
void backtracking(int cnt, vector<int>& nums, vector<bool>& flags, vector<int>& current) {
if (cnt == nums.size()) {
res.push_back(current);
}
else {
for (int i = 0; i < nums.size(); ++i) {
if (flags[i]) continue;
flags[i] = true;
current.push_back(nums[i]);
backtracking(cnt + 1, nums, flags, current);
flags[i] = false;
current.pop_back();
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
int n = nums.size();
vector<int> current;
vector<bool> flags(n, false);
backtracking(0, nums, flags, current);
return res;
}
};