-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079.单词搜索.java
81 lines (71 loc) · 2.17 KB
/
0079.单词搜索.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
/*
* @lc app=leetcode.cn id=79 lang=java
*
* [79] 单词搜索
*
* https://leetcode-cn.com/problems/word-search/description/
*
* algorithms
* Medium (39.40%)
* Likes: 273
* Dislikes: 0
* Total Accepted: 30.2K
* Total Submissions: 76.2K
* Testcase Example: '[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]\n"ABCCED"'
*
* 给定一个二维网格和一个单词,找出该单词是否存在于网格中。
*
* 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
*
* 示例:
*
* board =
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
*
* 给定 word = "ABCCED", 返回 true.
* 给定 word = "SEE", 返回 true.
* 给定 word = "ABCB", 返回 false.
*
*/
// @lc code=start
class Solution {
private int[][] visited;
private int correctLen = 0;
public boolean exist(char[][] board, String word) {
int rows = board.length;
int cols = board[0].length;
visited = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (helper(board, rows, cols, word, i, j)) {
return true;
}
}
}
return false;
}
private boolean helper(char[][] board, int rows, int cols, String word, int i, int j) {
if (correctLen == word.length()) {
return true;
}
boolean res = false;
if (0 <= i && i < rows && 0 <= j && j < cols && visited[i][j] == 0 && board[i][j] == word.charAt(correctLen)) {
correctLen += 1;
visited[i][j] = 1;
res = helper(board, rows, cols, word, i - 1, j) ||
helper(board, rows, cols, word, i + 1, j) ||
helper(board, rows, cols, word, i, j - 1) ||
helper(board, rows, cols, word, i, j + 1);
if (res == false) {
correctLen -= 1;
visited[i][j] = 0;
}
}
return res;
}
}
// @lc code=end