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

    [
      ["ABCE"],
      ["SFCS"],
      ["ADEE"]
    ]
    

    word = "ABCCED", -> returns true,
    word = "SEE", -> returns true,
    word = "ABCB", -> returns false.

    https://oj.leetcode.com/problems/word-search/

    思路:4个方向dfs,

    public class Solution {
        public boolean exist(char[][] board, String word) {
            if (word == null || board == null || word.equals(""))
                return false;
    
            int m = board.length;
            int n = board[0].length;
            boolean visit[][] = new boolean[m][n];
    
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++) {
                    boolean res = dfs(board, visit, i, j, word, 0);
                    if (res)
                        return true;
                }
    
            return false;
    
        }
    
        private int offset[][] = new int[][] { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } };
    
        private boolean dfs(char[][] board, boolean[][] visit, int x, int y, String word, int cur) {
            // System.out.println("dfs:"+x+","+y+","+cur);
    
            if (board[x][y] != word.charAt(cur))
                return false;
    
            if (cur == word.length() - 1) {
                return true;
            }
    
            int m = board.length;
            int n = board[0].length;
    
            visit[x][y] = true;
            int nx, ny;
            for (int i = 0; i < 4; i++) {
                nx = x + offset[i][0];
                ny = y + offset[i][1];
    
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) {
                    boolean res = dfs(board, visit, nx, ny, word, cur + 1);
                    if (res)
                        return true;
                }
    
            }
            visit[x][y] = false;
    
            return false;
    
        }
    
        public static void main(String[] args) {
            char[][] board;
            String word;
            board = new char[][] { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };
            word = "ASA";
            System.out.println(new Solution().exist(board, word));
    
            board = new char[][] { { 'b' } };
            word = "b";
            System.out.println(new Solution().exist(board, word));
    
        }
    
    }
    View Code

    第三遍记录: dfs比较经典的题目。

      注意这个dfs有返回值,每次递归的时候要先判断结果,如果已经返回true,后面就不执行了。

      注意vis[][]数组不要遗漏了和 递归时 true和false的设置时机。

      注意dfs向下递归时,先判断nx,ny的范围 和 vis数组,(有些题目可能还有其他限制条件)

      注意递归终止条件,多用小case头脑风暴测试下。

       

  • 相关阅读:
    西瓜书第三章-线性回归模型
    西瓜书第三章-线性模型【Logistic回归】(对数几率回归)
    Matlab绘图(二)低频率命令总结
    LaTex 资源总结
    plsql developer安装和使用遇到的问题解决
    【maven依赖包版本号是unknown导致无法下载】问题解决
    解决TortoiseGit报错:git did not exit cleanly (exit code 128)
    os模块的常用方法使用
    pycharm总是没有输出结果只是Process finished with exit code 0
    Jmeter发送post请求,报错:Content type 'text/plain;charset=UTF-8' not supported
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3815486.html
Copyright © 2011-2022 走看看