blob: bfb3c83cf697d347bfc873f31a0172f0950fee5d (
plain)
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
|
#include <vector>
using std::vector;
class Solution {
public:
void dfs(const vector<int> &nums, vector<int> ¤t, bool *num_used,
vector<vector<int>> &result) {
if (current.size() == nums.size()) {
result.push_back(current);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (!num_used[i]) {
num_used[i] = true;
current.push_back(nums[i]);
dfs(nums, current, num_used, result);
current.pop_back();
num_used[i] = false;
}
}
}
vector<vector<int>> permute(vector<int> &nums) {
vector<int> current;
vector<vector<int>> result;
bool *num_used = new bool[nums.size()]{false};
dfs(nums, current, num_used, result);
delete[] num_used;
return result;
}
};
|