• 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"));
        }
    
    }
  • 相关阅读:
    Add a Simple Action using an Attribute 使用特性添加简单按钮
    通俗易懂,什么是.NET/.NET Framework/.NET Core/.Net Standard?
    一文详解微服务架构
    我是如何失去团队掌控的?
    如何高效的学习技术
    Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
    设计模式学习笔记 ———— 简单工厂模式
    #ifndef/#define/#endif使用详解
    模块化开发
    源代码生成可执行文件的内部机理
  • 原文地址:https://www.cnblogs.com/null00/p/5094747.html
Copyright © 2020-2023  润新知