-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.全排列.ts
More file actions
40 lines (36 loc) · 1000 Bytes
/
Copy path46.全排列.ts
File metadata and controls
40 lines (36 loc) · 1000 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
31
32
33
34
35
36
37
38
39
/*
* @lc app=leetcode.cn id=46 lang=typescript
*
* [46] 全排列
*/
// @lc code=start
function permute(nums: number[]): number[][] {
let ans = []
const k = nums.length
// 需要利用一个 stack 记录访问过了那些节点
let visited = new Array(nums.length).fill(false)
const curr = (path) => {
// 遍历到叶子结点了,可以返回了
if (path.length === k) {
// make a deep copy since otherwise we'd be append the same list over and over
ans.push(path.slice())
return
}
for (let i = 0; i < nums.length; i++) {
// 如果没遍历过
if (!visited[i]) {
// 压入 path 数组,然后是否遍历过的数组此下标处变为 true
path.push(nums[i])
visited[i] = true
// 继续 dfs,直到最后一层
curr(path)
// 进行回溯,还原,以便下一次使用
visited[i] = false
path.pop()
}
}
}
curr([])
return ans
};
// @lc code=end