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 ...
随机推荐
- 军哥 LNMP 常见问题
安装memcached出错: Install memcached...Notice: memcached-1.4.25.tar.gz not found!!!download now...--2016 ...
- RK3288 USB UVC camera 摄像头 VIDIOC_DQBUF Failed!!! err[I/O error]
RK3288 Android5.1 多个品牌USB摄像头 同一块主板和代码,大部分品牌的USB摄像头可以正常使用,只有某一款USB摄像头不能使用. 插上摄像头,底层可以识别到摄像头. &l ...
- python3之es+log+date+timezone
from dateutil.parser import parse # 使用它可以方便的将字符串解析为datetimefrom tzlocal import get_localzone # 使用它可以 ...
- Erlang generic standard behaviours -- gen_server hibernate
hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...
- 3069: [Pa2011]Hard Choice 艰难的选择
Description Byteasar是一个很纠结的人.每次他经过Bytetown的时候都知道有至少2条不同的路径可以选择,这导致他必须花很长时间来决定走哪条路.Byteasar最近听说了Bytet ...
- http遇到的那些坑,iis上传文件报413错误 asp.net MVC
话不多说,直接上解决方法. 修改配置文件 applicationHost.config 具体地址在C:\Windows\System32\inetsrv\config 按照下面的节点进行 添加&l ...
- Error 20002 (severity 9):Adaptive Server connection failed
环境: Ubuntu12.10_x64 问题: 用tsql访问SQL Server >> tsql -H U sa Error (severity ): Adaptive Server c ...
- python学习 (三十五) logging
1 demo import logging # log level: # DEBUG # INFO # Warning # Error # CRITICAL logging.basicConfig( ...
- poj2356 Find a multiple
/* POJ-2356 Find a multiple ----抽屉原理 Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 华为交换机S5700 vty 0 4
最佳答案 vty 0 4 代表有5条VTY线路 由0到4 这个要看设备的版本有的设备会有更多条VTY线路比如一些企业版的设备 显示用户界面 进入TELNET 设置模式 user-interface ...