zoukankan      html  css  js  c++  java
  • 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"));
        }
    
    }
  • 相关阅读:
    【Linux】Linux服务器(centos7)安装配置 redis
    【java】使用 starter 的方式在 SpringBoot 中整合 Shiro
    【Docker】使用 Docker 基于centos7 构建 java 环境容器
    c#经典三层框架中的SqlHelper帮助类
    SOD框架的Model、连接数据库及增删改查
    用bat文件更改ip地址
    postgresql 创建并使用uuid作为唯一主键
    postgresql 查询字符串中是否包含某字符的几种写法
    pycharm激活码
    c# DataTable第二行改为各列标题字段
  • 原文地址:https://www.cnblogs.com/null00/p/5094747.html
Copyright © 2011-2022 走看看