-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path46. Permutations.cpp
More file actions
27 lines (27 loc) · 986 Bytes
/
46. Permutations.cpp
File metadata and controls
27 lines (27 loc) · 986 Bytes
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
class Solution {
public:
//https://leetcode.com/problems/permutations/
// 1 2 3 -- 2 1 3 -- 3 2 1 ....
//本质上是一个DFS,深度优先
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> >result;
permuteHelper(0,nums,result);
return result;
}
void permuteHelper(int start,vector<int>& nums,vector<vector<int> >&result){
if(start==nums.size()){
result.push_back(nums);
return;
}
for(int i=start;i<nums.size();i++){
swap(nums[i],nums[start]);
permuteHelper(start+1,nums,result);
swap(nums[i],nums[start]);
}
}
};
//本次一次通过,bug-free
//permuteHelper(start+1,nums,result); 这个地方不是 permuteHelper(i+1,nums,result);
// 已经失误好多遍了
// 123 132 213 -- 这个时候接下来 是 321 ,所以不对, 好题目
// 因为变化位置了,所以i就不是在原来位置上,只能够start+1啊