首页 > 其他 > 详细

79. Word Search

时间:2017-09-27 09:42:03      阅读:248      评论:0      收藏:0      [点我收藏+]
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

原文:http://www.cnblogs.com/asuran/p/7599829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!