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
  • 相关阅读:
    前端代码异常日志收集与监控
    基于window.onerror事件 建立前端错误日志
    MySQL数据类型和常用字段属性总结
    MySQL中char(36)被认为是GUID导致的BUG及解决方案
    dl,dt,dd,ul,li,ol区别
    泛型
    EF里Guid类型数据的自增长、时间戳和复杂类型的用法
    EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
    EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子
    SQL JOIN
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9000144.html
Copyright © 2011-2022 走看看