• 79. 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.

    Example:

    board =
    [
      ['A','B','C','E'],
      ['S','F','C','S'],
      ['A','D','E','E']
    ]
    
    Given word = "ABCCED", return true.
    Given word = "SEE", return true.
    Given word = "ABCB", return false.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    一般的dfs都是主函数中调用一次就行了。搜索单词时,每个都要调,因为每个点都有可能成为起点。

    [一句话思路]:

    单词的回溯法是dfs的一种,模式是:访问过-扩展-没访问过

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    单词的回溯法是dfs的一种,模式是:访问过-扩展-没访问过

    [复杂度]:Time complexity: O(4^n) Space complexity: O(n^2)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    每个点都有可能开始:

    //for loop
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (word.charAt(0) == board[i][j] && backtrace(board, i, j, word, 0)) return true;
                }
            }

    boolean == true不用写,就是默认== true

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

    2: trie树

     [代码风格] :

    class Solution {
        static boolean[][] visited;
        public boolean exist(char[][] board, String word) {
            //ini:visited
            visited = new boolean[board.length][board[0].length];
            
            //for loop
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (word.charAt(0) == board[i][j] && backtrace(board, i, j, word, 0)) return true;
                }
            }
            
            return false;
        }
        
        public boolean backtrace(char[][] board, int i, int j, String word, int index) {
            //length
            if (index == word.length()) return true;
            
            //exit:range, not equal, visited
            if (i < 0 || i >= board.length || j < 0 || j >= board[0].length ||
                word.charAt(index) != board[i][j] || visited[i][j]) return false;
            
            //expand
            visited[i][j] = true;
            if (backtrace(board, i + 1, j, word, index + 1) ||
                backtrace(board, i - 1, j, word, index + 1) ||
                backtrace(board, i, j + 1, word, index + 1) ||
                backtrace(board, i, j - 1, word, index + 1)) return true;
            visited[i][j] = false;
            
            return false;
        }
    }
    View Code
  • 相关阅读:
    使用ActivityGroup来切换Activity和Layout
    Fragment
    [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
    [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence
    [Swift]LeetCode296. 最佳开会地点 $ Best Meeting Point
    [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
    [Swift]LeetCode293. 翻转游戏 $ Flip Game
    [Swift]LeetCode291. 单词模式 II $ Word Pattern II
    [Postman]发出SOAP请求(18)
    [Postman]生成代码段(17)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9000144.html
Copyright © 2020-2023  润新知