-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP0079_WordSearch.java
82 lines (79 loc) · 2.96 KB
/
P0079_WordSearch.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package yyl.leetcode.p00;
/**
* <h3>单词搜索</h3><br>
* 给定一个二维网格和一个单词,找出该单词是否存在于网格中。<br>
* 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。<br>
* 同一个单元格内的字母不允许被重复使用。<br>
*
* <pre>
* 示例:
* board =
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
* 给定 word = "ABCCED", 返回 true.
* 给定 word = "SEE", 返回 true.
* 给定 word = "ABCB", 返回 false.
* </pre>
*/
public class P0079_WordSearch {
public static void main(String[] args) {
Solution solution = new Solution();
char[][] board = {//
{'A', 'B', 'C', 'E'}, //
{'S', 'F', 'C', 'S'}, //
{'A', 'D', 'E', 'E'}//
};
System.out.println(solution.exist(board, "ABCCED"));
System.out.println(solution.exist(board, "SEE"));
System.out.println(solution.exist(board, "ABCB"));
}
// 回溯+剪枝
// 首先查找到首字母,然后从该位置向4个方向遍历
// 时间复杂度:O(N*M),N为二维网格的元素个数,M为字符串长度
// 空间复杂度:O(N),N为二维网格的元素个数
static class Solution {
public boolean exist(char[][] board, String word) {
if (word.isEmpty()) {
return true;
}
if (board.length == 0 || board[0].length == 0) {
return false;
}
int m = board.length;
int n = board[0].length;
if (m * n < word.length()) {
return false;
}
boolean[][] visited = new boolean[m][n];
for (int y = 0; y < m; y++) {
for (int x = 0; x < n; x++) {
if (search(board, m, n, y, x, visited, word, 0)) {
return true;
}
}
}
return false;
}
private boolean search(char[][] board, int m, int n, int y, int x, boolean[][] visited, String word, int p) {
if (p == word.length()) {
return true;
}
if (y < 0 || x < 0 || y >= m || x >= n || visited[y][x] || board[y][x] != word.charAt(p)) {
return false;
}
visited[y][x] = true;
if (search(board, m, n, y - 1, x, visited, word, p + 1)// up
|| search(board, m, n, y + 1, x, visited, word, p + 1)// down
|| search(board, m, n, y, x - 1, visited, word, p + 1)// left
|| search(board, m, n, y, x + 1, visited, word, p + 1)// right
) {
return true;
}
visited[y][x] = false;
return false;
}
}
}