-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path046.cpp
32 lines (31 loc) · 839 Bytes
/
046.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
class Solution {
public:
// 回溯法
void backtracking(vector<vector<int> > &res, vector<int> &nums, vector<int> local, vector<bool> &used)
{
int len = nums.size();
if(local.size() == len)
{
res.push_back(local);
return;
}
for(int i=0; i<len; i++)
{
if(!used[i])
{
used[i] = true;
local.push_back(nums[i]);
backtracking(res, nums, local, used);
local.pop_back();
used[i] =false;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> > res;
vector<int> local;
vector<bool> used(nums.size(), false);
backtracking(res, nums, local, used);
return res;
}
};