-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode46.cpp
58 lines (51 loc) · 1.75 KB
/
leetcode46.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// idea: use visited to judge that whether current number has been added to list
// we don't use start index here because each time of recursion we would iterate nums again
// beats over 16% of submission
class Solution {
private:
void dfs(vector<int>& nums, vector<bool>& visited, vector<int>& sub, vector<vector<int>>& res){
if(sub.size() == nums.size()){
res.push_back(sub);
return;
}
for(int i = 0; i < nums.size(); ++i){
if(visited.at(i)) continue;
sub.push_back(nums.at(i));
visited.at(i) = true;
dfs(nums, visited, sub, res);
sub.pop_back();
visited.at(i) = false;
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<int> sub;
vector<bool> visited(nums.size(), false);
vector<vector<int>> res;
dfs(nums, visited, sub, res);
return res;
}
};
}
// idea: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
// beats over 86% of submission
class Solution {
private:
void dfs(vector<int>& nums, int start, vector<vector<int>>& res){
if(start == nums.size() - 1){
res.push_back(nums);
return;
}
for(int i = start; i < nums.size(); ++i){
swap(nums.at(start), nums.at(i));
dfs(nums, start+1, res);
swap(nums.at(start), nums.at(i));
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
dfs(nums, 0, res);
return res;
}
};