-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path46.cpp
More file actions
26 lines (25 loc) · 788 Bytes
/
Copy path46.cpp
File metadata and controls
26 lines (25 loc) · 788 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
class Solution {
public:
vector<vector<int>> permute(vector<int> &nums) {
vector<int> visited(nums.size(), 0);
vector<vector<int>> ret;
vector<int> temp;
BackTracking(nums, visited, temp, nums.size(), ret);
return ret;
}
void BackTracking(vector<int> &nums, vector<int> &visited, vector<int> &temp, int n, vector<vector<int>> &ret) {
if (n == 0) {
ret.push_back(temp);
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (visited[i] == 0) {
visited[i] = 1;
temp.push_back(nums[i]);
BackTracking(nums, visited, temp, n - 1, ret);
temp.pop_back();
visited[i] = 0;
}
}
}
};