• LeetCode


    题目:

    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.

    思路:

    递归;及时return true,减少计算量

    package recursion;
    
    public class WordSearch {
    
        public boolean exist(char[][] board, String word) {
            int m;
            int n;
            int wordLen;
            if (board == null || word == null || 
                    (m = board.length) == 0 || (n = board[0].length) == 0 || 
                    (wordLen = word.length()) == 0 || wordLen > m * n) 
                return false;
            boolean[][] visited = new boolean[m][n];
    
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (dfs(board, i, j, visited, word, 0, m, n, wordLen))
                        return true;
                }
            }
            return false;
        }
     
        private boolean dfs(char[][] board, int x, int y, boolean[][] visited, String word, int pos, int m, int n, int wordLen) {
            if (visited[x][y] || board[x][y] != word.charAt(pos)) return false;
            if (pos == wordLen - 1) return true;
            visited[x][y] = true;
    
            if (x - 1 >= 0 && dfs(board, x - 1, y, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (x + 1 < m && dfs(board, x + 1, y, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (y - 1 >= 0 && dfs(board, x, y - 1, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (y + 1 < n && dfs(board, x, y + 1, visited, word, pos + 1, m, n, wordLen)) return true;
            
            visited[x][y] = false;
            
            return false;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            char[][] board = 
                            {
                                {'A','B','C','E'},
                                {'S','F','C','S'},
                                {'A','D','E','E'}
                            };
            WordSearch w = new WordSearch();
            System.out.println(w.exist(board, "ABCCED"));
            System.out.println(w.exist(board, "SEE"));
            System.out.println(w.exist(board, "ABCB"));
        }
    
    }
  • 相关阅读:
    HttpRunner学习3--extract提取数据和引用
    利用Fiddler对Jmeter的请求进行抓包
    HttpRunner学习2--用例格式和简单使用
    HttpRunner学习1--Windows&Linux安装httprunner
    Linux日志中如何查找关键字及其前后的信息
    Linux命令学习-cat命令
    Linux下安装MySQL 5.7
    阿里云服务器修改主机名
    Linux下设置mysql允许远程连接
    利用PyInstaller打包exe文件
  • 原文地址:https://www.cnblogs.com/null00/p/5094747.html
Copyright © 2020-2023  润新知