-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation.cpp
39 lines (32 loc) · 916 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
#include <vector>
using namespace std;
// Recursive version
class Solution {
public:
vector<vector<int> > permute(vector<int> &num)
{
vector<vector<int>> permutations;
stack.resize(num.size(), 0);
visit.resize(num.size(), false);
do_permute(num, 0, permutations);
return permutations;
}
void do_permute(const vector<int> &num, int depth, vector<vector<int>> &result)
{
if (depth == num.size()) {
result.push_back(stack);
return;
}
for (int i = 0; i < num.size(); ++i) {
if (!visit[i]) {
visit[i] = true;
stack[depth] = num[i];
do_permute(num, depth + 1, result);
visit[i] = false; // we need to restore the visit array.
}
}
}
private:
vector<int> stack;
vector<bool> visit;
};