[抄题]:

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;
}
}

79. Word Search在字母矩阵中查找单词的更多相关文章

  1. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

  2. word search(二维数组中查找单词(匹配字符串))

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  3. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  4. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  5. 【一天一道LeetCode】#79. Word Search

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

随机推荐

  1. 执行npm install报错:npm ERR! code EINTEGRITY

    命令行执行npm install报错如下: D:\frontend\viewsdev>npm install npm ERR! code EINTEGRITY npm ERR! sha512-8 ...

  2. Memory stream is not expandable

    发现项目有一个地方在做图片缩放剪切的一个操作中.碰到有一些特殊的图片会报 Memory stream is not expandable 的错误 跟踪的时候发现是 由方法 originalStream ...

  3. Unit02: JDBC核心API

    Unit02: JDBC核心API db.properties 注意:如果使用连接池,可以在这个文件中增加对连接池的相关设置: 连接池参数,常用参数有: 初始连接数 最大连接数 最小连接数 每次增加的 ...

  4. mqtt 异步消息 长连接 解析

    mqtt 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放,简单,轻量级,且易于实现,这些优点使得他受用于任何环境 该协议的特点有: 使用发布/订阅消息的模式,提供一对多的消息发布,解除应用 ...

  5. Jenkins的构建

    每个项目的详情页会显示下图内容  左侧操作项没有太多需要说明,Changes选项在Multibranch pipeline的时候,会显示从git上获取到的提交历史,普通的pipeline还没有尝试 下 ...

  6. NOIP2013 Day1

    1.转圈游戏 https://www.luogu.org/problem/show?pid=1965 这道题失误极大,把freopen注释掉了,导致第一题暴0. 注意:在考试时一定要留下最后的时间检查 ...

  7. 关于PIPELINE,也许你应该知道

    所谓流水线(pipeline)设计,通常来说是流水生产线. 同样,组合逻辑路径可以看作是一条生产线,路径上的每个逻辑单元都可以看作是一个阶段,都会产生时延. 如果不采用流水设计,前后级组合逻辑依次工作 ...

  8. 用命令行方式关闭CentOS防火墙

    #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport 22 -j A ...

  9. 十三 Thread的一些常用方法

    setName() : 给线程起名字. isAlive() : 线程是否存或. currentThread() : 取得当前线程. getId() : 取得线程的唯一标识.

  10. ALSA声卡笔记3--ASoC驱动重要结构体关系图

    1.ASoC中重要的数据结构之间的关联方式 (1)Kernel-2.6.35-ASoC中各个结构的静态关系 ASoC把声卡实现为一个Platform Device,然后利用Platform_devic ...