-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpermutations.cpp
More file actions
43 lines (39 loc) · 1.07 KB
/
permutations.cpp
File metadata and controls
43 lines (39 loc) · 1.07 KB
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
#include <iterator>
#include "leetcode.h"
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
if (num.size() == 1) {
result.push_back(num);
return result;
}
for (size_t i = 0; i < num.size(); ++i ) {
vector<int> rest;
for(size_t j = 0; j < num.size(); ++j) {
if (j != i) rest.push_back(num[j]);
}
auto partial = permute(rest);
for(auto it = partial.begin(); it != partial.end(); it++) {
// note: inserting to front of vector is inefficient
// only for the sake of matching the OJ's answer order
it->insert(it->begin(), num[i]);
result.push_back(*it);
}
}
return result;
}
};
int main(int argc, char const *argv[])
{
Solution sol;
int A[] = {1, 2, 3};
int n = sizeof(A) / sizeof(int);
vector<int> num(A, A + n);
auto result = sol.permute(num);
for (auto row : result) {
copy(row.begin(), row.end(), ostream_iterator<int>(cout, " "));
cout << '\n';
}
return 0;
}