zoukankan      html  css  js  c++  java
  • [LC] 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.

    Example:

    board =
    [
      ['A','B','C','E'],
      ['S','F','C','S'],
      ['A','D','E','E']
    ]
    
    Given word = "ABCCED", return true.
    Given word = "SEE", return true.
    Given word = "ABCB", return false.

    Time: O(M * N * 4^|Word|)
     1 class Solution {
     2     int row;
     3     int col;
     4     public boolean exist(char[][] board, String word) {
     5         row = board.length;
     6         col = board[0].length;
     7         boolean[][] visited = new boolean[row][col];
     8         for (int i = 0; i < row; i++) {
     9             for (int j = 0; j < col; j++) {
    10                 if (helper(board, visited, word, i, j, 0)) {
    11                     return true;
    12                 }
    13             }
    14         }
    15         return false;
    16     }
    17     
    18     private boolean helper(char[][] board, boolean[][] visited, String word, int i, int j, int index) {
    19         if (index == word.length()) {
    20             return true;
    21         }
    22         if (i < 0 || i >= row || j < 0 || j >= col) {
    23             return false;
    24         }
    25         if (word.charAt(index) == board[i][j] && !visited[i][j]) {
    26             visited[i][j] = true;
    27             boolean res = helper(board, visited, word, i + 1, j, index + 1) ||
    28                 helper(board, visited, word, i - 1, j, index + 1) ||
    29                 helper(board, visited, word, i, j + 1, index + 1) ||
    30                 helper(board, visited, word, i, j - 1, index + 1);
    31             // need to clean up visited
    32             visited[i][j] = false;
    33             return res;
    34         }
    35 
    36         return false;
    37     }
    38 }
    class Solution {
        int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        public boolean exist(char[][] board, String word) {
            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 (dfs(0, word, board, visited, i, j)) {
                        return true;   
                    }
                }
            }
            return false;
        }
        
        private boolean dfs(int index, String word, char[][] board, boolean[][] visited, int row, int col) {
            if (index == word.length()) {
                return true;
            }
            // need to check edge after base case e.g. [['A']] 'A', is actually out of board
            if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || visited[row][col]) {
                    return false;
            }
            if (board[row][col] == word.charAt(index)) {
                visited[row][col] = true;
                for (int[] direction: directions) {
                    int nxtRow = direction[0] + row;
                    int nxtCol = direction[1] + col;
                    if (dfs(index + 1, word, board, visited, nxtRow, nxtCol)) {
                        return true;
                    }
                }
                visited[row][col] = false;
            }
            return false;
        }
    }
  • 相关阅读:
    ie浏览器下载附件中文乱码
    js 获取字符串中某字符第二次出现的下标
    uploadify附件上传 传参
    hibernate hql语句 注意事项
    违反检查约束条件
    xml中的<if>和截取字符串
    引用不了XXservice,怎么办?
    enctype="multipart/form-data"的form传参
    freemarker 嵌套循环 (导出word时,修改ftl模板)
    springboot 打包问题 项目依赖三方jar ,之前的同事是直接丢到项目lib下 ,今天使用springbioot打包的时候,找不到这个jar
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12005363.html
Copyright © 2011-2022 走看看