79. Word Search
阿新 • • 發佈:2017-09-27
true private search sed urn span ring cnblogs str
class Solution { public boolean exist(char[][] board, String word) { if(board.length==0||board[0].length==0) return false; boolean[][] used=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(search(i, j, board, used, word)) return true; return false; } private boolean search(int i, int j, char[][] board, boolean[][] used, String word){ if(word.length()==0) return true; if(i<0||i>=board.length||j<0||j>=board[0].length||used[i][j]==true||word.charAt(0)!=board[i][j]) return false; used[i][j]=true; boolean res=search(i-1,j,board,used,word.substring(1))||search(i+1,j,board,used,word.substring(1)) ||search(i,j-1,board,used,word.substring(1))||search(i,j+1,board,used,word.substring(1)); used[i][j]=false; return res; } }
79. Word Search