leetCode 79.Word Search (词搜索) 解题思路和方法

来源:互联网 发布:幺正矩阵 编辑:程序博客网 时间:2024/06/05 13:26

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.


思路:此题意思是下个字符必须与上个字符相邻,且不能重复使用。代码上难度不大,最主要的是控制搜索的方向,分别向四个方向搜索即可。

代码如下:

public class Solution {    boolean[][] b;    public boolean exist(char[][] board, String word) {        if(word.length() == 0){            return true;        }        for(int i = 0; i < board.length;i++){            for(int j = 0; j < board[0].length; j++){                if(board[i][j] == word.charAt(0)){                    b = new boolean[board.length][board[0].length];                    if(search(board,i,j,b,0,word)){                        return true;                    }                }            }        }        return false;    }            //根据首字母的i,j位置查找word    private boolean search(char[][] board,int i,int j,boolean[][] b,int index,String word){        if(board[i][j] != word.charAt(index)){//字符不相等。返回false            return false;        }        if(index == word.length() - 1){//如果到达最后一个,返回true        return true;        }                b[i][j] = true;//标记已使用                if(i > 0 && !b[i-1][j] && search(board,i-1,j,b,index+1,word)){//如果i>0,且i-1的值没有被使用,搜索            return true;        }                if(i < board.length-1 && !b[i+1][j] && search(board,i+1,j,b,index+1,word)){//如果没有边界,且i+1的值没有被使用,搜索            return true;        }                if(j > 0 && !b[i][j-1] && search(board,i,j-1,b,index+1,word)){//如果j>0,且j-1的值没有被使用,搜索            return true;        }                if(j < board[0].length-1 && !b[i][j+1] && search(board,i,j+1,b,index+1,word)){//如果没到边界,且j+1的值没有被使用,搜索            return true;        }        b[i][j] = false;        return false;    }    }


0 0
原创粉丝点击