forked from jnozsc/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations.cpp
45 lines (42 loc) · 1.06 KB
/
Permutations.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
/*
Given a list of numbers, return all possible permutations.
Example
For nums [1,2,3], the permutaions are:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > permute(vector<int>& nums) {
vector<vector<int> > result;
if (nums.empty()) {
return result;
}
vector<int> current;
permuteDFS(result, current, nums);
// write your code here
return result;
}
void permuteDFS(vector<vector<int> >& result, vector<int> current, vector<int> nums) {
if (current.size() == nums.size()) {
result.push_back(current);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (current.empty() || find(current.begin(), current.end(), nums[i]) == current.end()) {
current.push_back(nums[i]);
permuteDFS(result, current, nums);
current.pop_back();
}
}
}
};