Skip to content

Commit 9741932

Browse files
committed
79.单词搜索,回溯
1 parent 6417924 commit 9741932

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
39. 组合总和
2323
40. 组合总和 II
2424
41. 缺失的第一个正数
25+
42. 接雨水
2526
46. 全排列
2627
47. 全排列 II
2728
48. 旋转图像
28-
42. 接雨水
2929
51. N 皇后
3030
53. 最大子数组和
3131
54. 螺旋矩阵
@@ -41,6 +41,7 @@
4141
76. 最小覆盖子串
4242
77. 组合
4343
78. 子集
44+
79. 单词搜索
4445
82. 删除排序链表中的重复元素 II
4546
84. 柱状图中最大的矩形
4647
85. 最大矩形

leetcode_Java/DoneType.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
51. N 皇后
4545
77. 组合
4646
78. 子集
47+
79. 单词搜索
4748
90. 子集 II
4849
93. 复原 IP 地址
4950
131. 分割回文串
@@ -116,6 +117,7 @@
116117

117118

118119
五、深度优先搜索
120+
79. 单词搜索
119121
130. 被围绕的区域
120122
200. 岛屿数量
121123
463. 岛屿的周长

leetcode_Java/Solution0079.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 79. 单词搜索
2+
3+
4+
/*
5+
深度优先搜索,回溯:
6+
1、把递归方法用到的公共变量作为成员变量,避免入参冗杂,入参只保留方法独用的局部变量
7+
2、遍历字符网格,以每个字符作为单词搜索的起点,搜索成功返回true,遍历完后仍未搜索成功则返回false
8+
3、定义递归函数:
9+
1)终止条件:
10+
① 索引等于单词长度,表示搜索成功结束,返回true
11+
② 网格越界、网格字符已访问、网格字符与单词字符不同,返回false
12+
2)递归回溯:
13+
① 未达到终止条件前,需要继续搜索。先标记当前位置已访问,然后从上下左右四个方向继续搜索,获取搜索结果进行或运算,表示其中一个搜索成功即为成功
14+
② 如果四个方向都搜索失败,需要重新标记当前位置未访问,不影响其他递归搜索,即回溯
15+
③ 将搜索结果返回给上一层
16+
*/
17+
class Solution {
18+
private char[][] board;
19+
private String word;
20+
private int m, n;
21+
private boolean[][] visited;
22+
23+
public boolean exist(char[][] board, String word) {
24+
this.board = board;
25+
this.m = board.length;
26+
this.n = board[0].length;
27+
this.word = word;
28+
this.visited = new boolean[m][n];
29+
for (int row = 0; row < m; row++) {
30+
for (int col = 0; col < n; col++) {
31+
if (dfs(0, row, col)) {
32+
return true;
33+
}
34+
}
35+
}
36+
return false;
37+
}
38+
39+
private boolean dfs(int index, int row, int col) {
40+
if (index == word.length()) {
41+
return true;
42+
}
43+
if (row < 0 || row >= m || col < 0 || col >= n || visited[row][col] || word.charAt(index) != board[row][col]) {
44+
return false;
45+
}
46+
visited[row][col] = true;
47+
boolean res = dfs(index + 1, row + 1, col)
48+
|| dfs(index + 1, row - 1, col)
49+
|| dfs(index + 1, row, col + 1)
50+
|| dfs(index + 1, row, col - 1);
51+
if (!res) {
52+
visited[row][col] = false;
53+
}
54+
return res;
55+
}
56+
}

leetcode_Java/Solution0130.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public void solve(char[][] board) {
3030

3131
private void dfs(char[][] board, int row, int col) {
3232
int n = board.length, m = board[0].length;
33-
if (row < 0 || col < 0 || row >= n || col >= m) {
34-
return;
35-
}
36-
if (board[row][col] != 'O') {
33+
if (row < 0 || col < 0 || row >= n || col >= m || board[row][col] != 'O') {
3734
return;
3835
}
3936
board[row][col] = 'Y';

0 commit comments

Comments
 (0)