您的位置:首页 > 其它

[leetcode]Word Search

2014-07-15 19:20 190 查看
Word Search

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 =

[
["ABCE"],
["SFCS"],
["ADEE"]
]

word =
"ABCCED"
, -> returns
true
,
word =
"SEE"
, -> returns
true
,
word =
"ABCB"
, -> returns
false
.

时隔这么久,第二遍刷,居然跟第一遍的代码完全一样,连细节都一样,太不可思议了。

算法思路:

对上下左右四个方向进行递归,dfs,注意判断边界

public class Solution {
public boolean exist(char[][] board, String word) {
if(board == null || board.length == 0|| word == null ) return false;
int height = board.length;
int width = board[0].length;
if(height * width < word.length()) return false;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(board[i][j] == word.charAt(0)){
if(dfs(board,word.substring(1),i,j)) return true;
}
}
}
return false;
}
private boolean dfs(char[][] board,String left,int row,int column){
if(left.length() == 0){
return true;
}
int height = board.length;
int width = board[0].length;
char c = board[row][column];
board[row][column] = ' ';
if( column > 0 && board[row][column - 1] == left.charAt(0)){//go left
if( dfs(board, left.substring(1), row, column - 1)) return true;
}
if( column < width - 1 && board[row][column + 1] == left.charAt(0)){//go right
if( dfs(board, left.substring(1), row, column + 1)) return true;
}
if( row > 0 && board[row - 1][column] == left.charAt(0)){//go up
if( dfs(board, left.substring(1), row - 1, column) ) return true;
}
if( row < height - 1 && board[row + 1][column] == left.charAt(0)){//go down
if( dfs(board, left.substring(1), row + 1, column)) return true;
}
board[row][column] = c;
return false;
}
}


其实这道题这种做法是不严谨的,因为题中并未说明字符串中不包含空格,事实上最好开辟一个标记矩阵,来标记哪些点是否已经匹配过。

我的算法中对上下左右四个方向进行了直接的判断,我同学的做法,用一个数组来标记走向,很值得借鉴。戳这里


还是偷懒没有用visited数组标记:


这次最后一个case没过: board[][] = {{"a"}},word = "a"



public class Solution {
int[][] dir = {{0,1},{0,-1},{1,0},{-1,0}};
public boolean exist(char[][] board, String word) {
if(board == null || board.length == 0 || board[0].length == 0 || word == null || word.length() == 0) return false;
int height = board.length,width = board[0].length;
if(height * width < word.length()) return false;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(board[i][j] == word.charAt(0)){
if(dfs(board,word,i,j,height,width)) return true;
}
}
}
return false;
}
private boolean dfs(char[][] board,String word,int row,int col,int height,int width){
if(board[row][col] == word.charAt(0)){
board[row][col] = '~';
if(word.length() == 1) return true;//本来这个判断写在外面,判断word.length=0,配合下面的height & width合法判断,导致最后一个case没过
}else{
return false;
}
for(int i = 0; i < 4; i++){
int newRow = row + dir[i][0];
int newCol = col + dir[i][1];
if(newRow < height && newRow >= 0 && newCol < width && newCol >= 0 ){
if(dfs(board,word.substring(1),newRow,newCol,height,width)) return true;
}
}
board[row][col] = word.charAt(0);
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: