-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermute.cpp
More file actions
52 lines (43 loc) · 1.08 KB
/
permute.cpp
File metadata and controls
52 lines (43 loc) · 1.08 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
44
45
46
47
48
49
50
51
52
#include <vector>
using namespace std;
class Solution
{
public:
vector<vector<int>> permute(vector<int> &nums)
{
// 递归思想解决问题
vector<vector<int>> result;
vector<int> path;
int length = nums.size();
vector<bool> used(length,false);
// start是啥意思?遍历到第几层了
backtrack(result, nums, path, length,used);
return result;
}
inline void backtrack(vector<vector<int>> &result,vector<int> &nums,vector<int>& path,int length, vector<bool>& used)
{
//终止条件
if(path.size() == length)
{
result.push_back(path);
return;
}
for(int i = 0;i < length;i++)
{
//跳过已使用的数组
if(used[i]) continue;
used[i] = true;
path.push_back(nums[i]);
backtrack(result,nums,path,length,used);
path.pop_back();
used[i] = false;
}
}
};
int main()
{
Solution sl;
vector<int> a = {1,2,3};
sl.permute(a);
return 0;
}