79. Word Search

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.

public class Solution {
    public boolean exist(char[][] board, String word) {
        int m = board.length,n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        for(int i=0;i=board.length||j>=board[0].length)
          return false;
        if(visited[i][j])
          return false;
        if(board[i][j] != word.charAt(index))
          return false;
        int[][] direction = {{-1,0},{1,0},{0,1},{0,-1}};
        for(int k=0;k

你可能感兴趣的:(79. Word Search)