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
  • 相关阅读:
    UVA10163 Storage Keepers (动态规划)
    Uva12174 Shuffle(滑动窗口)
    上决╇ф人员分配问题 (背包问题)
    UVA
    UVALive
    poj1151 Atlantis (线段树+扫描线+离散化)
    poj2528 Mayor's posters (线段树+离散化)
    php 调用微信虚拟支付
    ptoto文件转换的java文件,报错 UnusedPrivateParameter
    redis 安装及启动时警告处理
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9000144.html
Copyright © 2011-2022 走看看