Word Search
2016-06-12 01:46
302 查看
和island number有相同之处,都是要dfs遍历周围的点,并设置新值,但是在前者不需要返回结果,后者的返回结果是或的关系,而自己一开始却是按照与的结果返回的,直接思路就错了。。
public class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word == null || word.length() == 0) {
return false;
}
boolean [][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (existHelper(board, word, i, j, 0, visited)) {
return true;
}
// if (word.charAt(0) == board[i][j]) {
// if (existHelper(board, word, i, j, pos + 1, visited)) {
// return true;
// }
// }
}
}
return false;
}
private boolean existHelper(char[][] board, String word, int i, int j, int pos, boolean [][] visited) {
if (pos >= word.length()) {
return false;
}
if (i< 0 || i >= board.length || j < 0 || j >= board[0].length) {
return false;
}
if (word.charAt(pos) != board[i][j] || visited[i][j]) {
return false;
}
visited[i][j] = true;
if (pos == word.length() - 1) {
return true;
}
boolean result = existHelper(board, word, i - 1, j, pos + 1, visited) || existHelper(board, word, i, j - 1, pos + 1, visited) || existHelper(board, word, i + 1, j, pos + 1, visited) || existHelper(board, word, i, j + 1, pos + 1, visited);
visited[i][j] = false;
return result;
// if (i - 1 >= 0 && word.charAt(pos) == board[i - 1][j] && !visited[i - 1][j]) {
// //visited[i - 1][j] = true;
// return existHelper(board, word, i - 1, j, pos + 1, visited);
// }
// if (j - 1 >= 0 && word.charAt(pos) == board[i][j - 1] && !visited[i][j - 1]) {
// //visited[i][j - 1] = true;
// return existHelper(board, word, i, j - 1, pos + 1, visited);
// }
// if (i + 1 < board.length && word.charAt(pos) == board[i + 1][j] && !visited[i + 1][j]) {
// //visited[i + 1][j] = true;
// return existHelper(board, word, i + 1, j, pos + 1, visited);
// }
// if (j + 1 < board[0].length && word.charAt(pos) == board[i][j + 1] && !visited[i][j + 1]) {
// //visited[i][j + 1] = true;
// return existHelper(board, word, i, j + 1, pos + 1, visited);
// }
// visited[i][j] = false;
// return false;
}
}
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
word =
-> returns
word =
-> returns
word =
-> returns
public class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word == null || word.length() == 0) {
return false;
}
boolean [][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (existHelper(board, word, i, j, 0, visited)) {
return true;
}
// if (word.charAt(0) == board[i][j]) {
// if (existHelper(board, word, i, j, pos + 1, visited)) {
// return true;
// }
// }
}
}
return false;
}
private boolean existHelper(char[][] board, String word, int i, int j, int pos, boolean [][] visited) {
if (pos >= word.length()) {
return false;
}
if (i< 0 || i >= board.length || j < 0 || j >= board[0].length) {
return false;
}
if (word.charAt(pos) != board[i][j] || visited[i][j]) {
return false;
}
visited[i][j] = true;
if (pos == word.length() - 1) {
return true;
}
boolean result = existHelper(board, word, i - 1, j, pos + 1, visited) || existHelper(board, word, i, j - 1, pos + 1, visited) || existHelper(board, word, i + 1, j, pos + 1, visited) || existHelper(board, word, i, j + 1, pos + 1, visited);
visited[i][j] = false;
return result;
// if (i - 1 >= 0 && word.charAt(pos) == board[i - 1][j] && !visited[i - 1][j]) {
// //visited[i - 1][j] = true;
// return existHelper(board, word, i - 1, j, pos + 1, visited);
// }
// if (j - 1 >= 0 && word.charAt(pos) == board[i][j - 1] && !visited[i][j - 1]) {
// //visited[i][j - 1] = true;
// return existHelper(board, word, i, j - 1, pos + 1, visited);
// }
// if (i + 1 < board.length && word.charAt(pos) == board[i + 1][j] && !visited[i + 1][j]) {
// //visited[i + 1][j] = true;
// return existHelper(board, word, i + 1, j, pos + 1, visited);
// }
// if (j + 1 < board[0].length && word.charAt(pos) == board[i][j + 1] && !visited[i][j + 1]) {
// //visited[i][j + 1] = true;
// return existHelper(board, word, i, j + 1, pos + 1, visited);
// }
// visited[i][j] = false;
// return false;
}
}
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]
word =
"ABCCED",
-> returns
true,
word =
"SEE",
-> returns
true,
word =
"ABCB",
-> returns
false.
相关文章推荐
- grouth:全栈工程师指南
- mysql教程
- CSDN第一篇:处女座
- 固定比例01环的均匀分布问题
- 典型用户
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- zabbix
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- Python实现N阶台阶的走法问题
- 使用video.js文件报错:Failed to load resource: net::ERR_FILE_NOT_FOUND的解决办法
- 利用Broadcast实现强制下线