-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation.cpp
41 lines (34 loc) · 954 Bytes
/
permutation.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
/***********************************
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
***********************************/
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > permute(vector<int> &num) {
res.clear();
permute(num, 0);
return res;
}
void permute(vector<int> &num, int start){
if(start == num.size()-1){
res.push_back(num);
return;
}
else if(start > num.size() - 1){
return;
}
for(int i = start; i < num.size(); i++){
int temp = num[start];
num[start] = num[i];
num[i] = temp;
permute(num, start+1);
num[i] = num[start];
num[start] = temp;
}
}
};