-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
executable file
·50 lines (46 loc) · 1.46 KB
/
Solution.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
package $079;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 12:11 2018/6/4.
*/
class Solution {
boolean res = false;
public boolean exist(char[][] board, String word) {
boolean[][] flag = new boolean[board.length][board[0].length];
char[] c = word.toCharArray();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != c[0]) {
continue;
}
search(c, 0, board, i, j, flag);
if (res){
return true;
}
}
}
return false;
}
private void search(char[] c, int i, char[][] board, int curRow, int curCol, boolean[][] flag) {
if (res || i == c.length) {
res = true;
return;
}
if (curRow < 0 || curRow >= board.length
|| curCol < 0 || curCol >= board[0].length
|| flag[curRow][curCol]
|| board[curRow][curCol] != c[i]) {
return;
}
flag[curRow][curCol] = true;
//right
search(c, i + 1, board, curRow, curCol + 1, flag);
//down
search(c, i + 1, board, curRow + 1, curCol, flag);
//left
search(c, i + 1, board, curRow, curCol - 1, flag);
//up
search(c, i + 1, board, curRow - 1, curCol, flag);
flag[curRow][curCol] = false;
}
}