-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path全排列.js
More file actions
30 lines (30 loc) · 738 Bytes
/
全排列.js
File metadata and controls
30 lines (30 loc) · 738 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
28
29
30
/**
* @param {number[]} nums
* @return {number[][]}
46. 全排列
[1,2,3] length=3 []
*/
var permute = function(nums) {
const dfs = (curr, store) => {
// 1.满足条件,记录结果
// 2.满足终止条件,立即终止
if(curr.length===nums.length) {
ans.push([...curr]);
// store.length === 0
return;
}
// 3.继续尝试
for(let i=0; i<store.length; i++) {
// i=0 i=1 i=2
//[] [1, 2, 3]
//[2] [1,3]
dfs(
curr.concat(store[i]),
store.slice(0,i).concat(store.slice(i+1))
)
}
};
let ans = [];
dfs([], nums);
return ans;
};