码迷,mamicode.com
首页 > 其他好文 > 详细

79. Word Search

时间:2017-09-27 10:00:25      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:code   substring   ret   []   bst   public   length   bsp   i++   

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

标签:code   substring   ret   []   bst   public   length   bsp   i++   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!