-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.h
More file actions
27 lines (25 loc) · 895 Bytes
/
Permutations.h
File metadata and controls
27 lines (25 loc) · 895 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
27
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > res;
generatePermutations(num, 0, res);
return res;
}
private:
void generatePermutations(vector<int> &curr, int pos, vector<vector<int> > &res) {
if(pos == curr.size()) {
res.push_back(curr);
}
for(int i = pos; i < curr.size(); ++i) {
vector<int> newv(curr);
//newv.erase(newv.begin() + i);
//newv.insert(newv.begin() + pos, curr[i]);
newv[pos] = newv[i];// because all the numbers are unique, we don't need to keep the order
newv[i] = curr[pos];// so swaping is okay here
generatePermutations(newv, pos+1, res);
//backtracking, not necesseary
newv[i] = newv[pos];
newv[pos] = curr[pos];
}
}
};