zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    VS Code C++ 代码格式化方法(clang-format)
    linux函数深入探索——open函数打开文件是否将文件内容加载到内存空间
    总结一下数据库的 一对多、多对一、一对一、多对多 关系
    netstat -st输出解析(二)
    Connection reset by peer的常见原因及解决办法
    Linux errno详解
    Linux之清理linux内存cache
    十大经典排序算法(动图演示)
    android studio导出apk
    多线程还是多进程的选择及区别
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9000144.html
Copyright © 2011-2022 走看看