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"));
        }
    
    }
  • 相关阅读:
    jmeter的断言
    Fiddler(五)设置显式IP地址
    学习pycharm----自动化接口
    fidder重复创建数据+模拟接口响应数据+fidder接口测试
    python网络/并发编程部分简单整理
    python面向对象部分简单整理
    python模块与包简单整理
    python函数部分整理
    Python基础部分整理
    Scheme Implementations对比
  • 原文地址:https://www.cnblogs.com/null00/p/5094747.html
Copyright © 2011-2022 走看看