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;
}
}
79. Word Search在字母矩阵中查找单词的更多相关文章
- 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 ...
- word search(二维数组中查找单词(匹配字符串))
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- 【一天一道LeetCode】#79. Word Search
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
随机推荐
- [嵌入式]I2C协议指东
最近闲来无聊,入了一块MPU6050,手头本来就有一块原子的STM32 MINI开发板,凑活着学习了一下IIC,特此总结. IIC,是集成电路总线[Inter-Intergrated Circuit] ...
- undefined vs. null
undefined vs. null 一.相似性 在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别. var a = undefined; var a = n ...
- 如何在 Linux 上使用 x2go 设置远程桌面
https://linux.cn/article-5708-1.html 由于一切都迁移到了云上,作为提高职员生产力的一种方式,虚拟远程桌面在工业中越来越流行.尤其对于那些需要在多个地方和设备之间不停 ...
- xshell 提示 继续使用此程序必须应用到最新的更新或使用新版本 的解决方案
当打开正在使用的xshell后,提示“继续使用此程序必须应用到最新的更新或使用新版本 ” 是因为我们正在使用的是xshell5 版本,需要我们再安装一个xshell6 版本 我个人使用的是家庭/教育 ...
- linux(7)
第十七单元 Samba服务 [本节内容]1. 掌握samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件.2. 掌握samba服务的启动.停止.重启service ...
- dubbox实现REST服务
一.dubbox的由来 dubbox是当当网基于dubbo的基础上开发的扩展版,也可以认为是dubbo的升级版,根据当前互联网的应用需求,增加了很多扩展的功能. dubbox并没有发布到maven中央 ...
- 使用maven打包额外的jar
当使用maven打包的时候,部分自己的额外使用的在maven基础库里面是没有的时候: 参考: https://blog.csdn.net/hguisu/article/details/5107268 ...
- 《Hadoop技术内幕》读书笔记——Task运行过程分析
本文是董西成的Hadoop技术内幕一书的读书章节总结. 第八章 Task运行过程分析 所有Task需要周期性地向TaskTracker汇报最新进度和计数器值,而这正是由Reporter组件实现的,其中 ...
- 卷积神经网络之VGG网络模型学习
VGG:VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION 牛津大学 visual geometry group(VG ...
- 论 html与css的关系
一.网页前端三剑客基础介绍 1-1 Html和CSS的关系学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1. HTM ...