Skip to content

Commit f9d2a31

Browse files
author
liguanliang1
committed
feat: backtracking
1 parent c46a926 commit f9d2a31

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

leetcode/backtracking/index.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var permute = function (nums) {
2+
let len = nums.length, res = [];
3+
if (!len) return res;
4+
5+
let used = []; // boolean[]
6+
let path = []; //number[]
7+
dfs(nums, len, 0, path, used, res);
8+
return res;
9+
10+
function dfs(nums, len, depth, path, used, res) {
11+
if (depth === len) {
12+
//path是动态数组,不能直接push,需要拷贝一份当前值保存到结果中
13+
res.push([...path]);
14+
return;
15+
}
16+
17+
// 针对全排列中下标为 depth 的位置进行所有可能的尝试
18+
for (let i = 0; i < len; i++) {
19+
if (!used[i]) {
20+
path.push(nums[i]);
21+
used[i] = true;
22+
23+
console.log(path, '22222')
24+
// 往下找全排列中的下一个位置
25+
dfs(nums, len, depth + 1, path, used, res);
26+
27+
// 形成一个全排列后,进行回退,尝试其他答案
28+
used[i] = false;
29+
path.pop();
30+
}
31+
}
32+
}
33+
};
34+
35+
// 八皇后
36+
var solveNQueens = function (n) {
37+
if (n == 0) return res;
38+
39+
let col = [], main = [], sub = []; // boolean[]
40+
let res = []; // string[]
41+
let path = []; //number[]
42+
dfs(0, path);
43+
return res;
44+
45+
function dfs(row, path) {
46+
// 深度优先遍历到下标为 n,表示 [0.. n - 1] 已经填完,得到了一个结果
47+
if (row == n) {
48+
const board = convert2board(path);
49+
res.push(board);
50+
return;
51+
}
52+
53+
// 针对下标为 row 的每一列,尝试是否可以放置
54+
for (let j = 0; j < n; j++) {
55+
if (!col[j] && !main[row - j + n - 1] && !sub[row + j]) {
56+
path.push(j);
57+
58+
// 记录该位置的攻击范围
59+
col[j] = true;
60+
main[row - j + n - 1] = true; //加n-1是为了防止数组索引为负数
61+
sub[row + j] = true;
62+
63+
// 进入下一行
64+
dfs(row + 1, path);
65+
66+
// 回溯, 去掉path中最后一个值,尝试其他选项
67+
col[j] = false;
68+
main[row - j + n - 1] = false;
69+
sub[row + j] = false;
70+
path.pop();
71+
}
72+
}
73+
}
74+
75+
// 输出一个结果
76+
function convert2board(path) {
77+
let board = []; // string[]
78+
for (let i = 0; i < path.length; i++) {
79+
let ret = new Array(n).fill('.');
80+
ret[path[i]] = 'Q';
81+
board.push(ret.join(''))
82+
}
83+
84+
return board;
85+
}
86+
};

leetcode/dynamic-programing/water-problem.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import LinkedList from '../link-list/LinkedList'
22

33

44
// 积水问题,使用单调栈的思想
5-
function trap(height) {
5+
function trap0(height) {
66
const stack = new LinkedList();
77
// 最少也需要3个柱子才能积水,否则直接返回0
88
if (height.length < 3) return 0;
@@ -33,7 +33,7 @@ function trap(height) {
3333
return res;
3434
}
3535

36-
const result = trap([4, 2, 0, 3, 2, 5])
36+
const result = trap0([4, 2, 0, 3, 2, 5])
3737
console.log('water-problem', result)
3838
// // 按层计算,负责度较高可能会超时
3939
// class Solution42_byLayer {
@@ -147,7 +147,7 @@ console.log('water-problem', result)
147147

148148
// 链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode/
149149
// 暴力解法
150-
function trap(height = []) {
150+
function trap1(height = []) {
151151
let ans = 0;
152152
let size = height.length;
153153
for (let i = 1; i < size - 1; i++) {

leetcode/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ import "./stringCalculate"
2121
import "./MathSqrt"
2222
import "./search-binary-tree/index"
2323
import "./link-list/rotateList"
24-
import "./dynamic-programing/water-problem"
24+
import "./dynamic-programing/water-problem"
25+
import "./backtracking"

0 commit comments

Comments
 (0)