Word Search

来源:互联网 发布:淘宝一个好评多少分 编辑:程序博客网 时间:2024/06/03 07:02

和island number有相同之处,都是要dfs遍历周围的点,并设置新值,但是在前者不需要返回结果,后者的返回结果是或的关系,而自己一开始却是按照与的结果返回的,直接思路就错了。。

public class Solution {    public boolean exist(char[][] board, String word) {        if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word == null || word.length() == 0) {            return false;        }        boolean [][] visited = 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 (existHelper(board, word, i, j, 0, visited)) {                        return true;                }                // if (word.charAt(0) == board[i][j]) {                //     if (existHelper(board, word, i, j, pos + 1, visited)) {                //         return true;                //     }                // }            }        }        return false;    }        private boolean existHelper(char[][] board, String word, int i, int j, int pos, boolean [][] visited) {        if (pos >= word.length()) {            return false;        }        if (i< 0 || i >= board.length || j < 0 || j >= board[0].length) {            return false;        }        if (word.charAt(pos) != board[i][j] || visited[i][j]) {            return false;        }        visited[i][j] = true;        if (pos == word.length() - 1) {            return true;        }                boolean result = existHelper(board, word, i - 1, j, pos + 1, visited) || existHelper(board, word, i, j - 1, pos + 1, visited) || existHelper(board, word, i + 1, j, pos + 1, visited) || existHelper(board, word, i, j + 1, pos + 1, visited);                visited[i][j] = false;        return result;        // if (i - 1 >= 0 && word.charAt(pos) == board[i - 1][j] && !visited[i - 1][j]) {        //     //visited[i - 1][j] = true;        //     return existHelper(board, word, i - 1, j, pos + 1, visited);        // }        // if (j - 1 >= 0 && word.charAt(pos) == board[i][j - 1] && !visited[i][j - 1]) {        //     //visited[i][j - 1] = true;        //     return existHelper(board, word, i, j - 1, pos + 1, visited);        // }        // if (i + 1 < board.length && word.charAt(pos) == board[i + 1][j] && !visited[i + 1][j]) {        //     //visited[i + 1][j] = true;        //     return existHelper(board, word, i + 1, j, pos + 1, visited);        // }        // if (j + 1 < board[0].length && word.charAt(pos) == board[i][j + 1] && !visited[i][j + 1]) {        //     //visited[i][j + 1] = true;        //     return existHelper(board, word, i, j + 1, pos + 1, visited);        // }        // visited[i][j] = false;        // return false;    }}

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 =

[  ['A','B','C','E'],  ['S','F','C','S'],  ['A','D','E','E']]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
0 0