单词搜索 II · Word Search II
[抄题]:
给出一个由小写字母组成的矩阵和一个字典。找出所有同时在字典和矩阵中出现的单词。一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动。
给出矩阵:
doaf
agai
dcan
和字典:
{"dog", "dad", "dgdg", "can", "again"}
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 用p指针进行比较,不知道指针应该怎么用,指针就是一个新类,利用其中的TrieNode[] next数组移动即可。表示节点的初始化,
p = p.next[c - 'a'];使节点等于初始化之后的值。- 为防止出现res中有相同单词,每添加一个单词后就设其为空, 不同路线相同结果时要想到
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

在trie的结构中,字母其实是线段和下一个节点共有的,所以trienode中的节点数组命名为next

建树时,每个单词都用p从root开始重新新建
[一刷]:
- dfs的参数在总函数中有用的变量一一对应就行了,数组名还是要有的
- next[],忘记了 数组括号中一定只能是数字,不能是别的,应该是c - 'a'
- board[i][j]直接= 字母c就行了,不用加‘’
- 纠结不清的:dfs的表达式上限是由总表达式的上限决定的,
i < board.length, dfs(i+1)也要带入后符合规则,故i < board.length - 1
[二刷]:
- 当前字母对应的节点p.next[c - 'a'];非空时,说明trie中已经存了和图中相对应的节点,p就应该指向当前对应的字母节点,否则新建。没有理解
- w循环时,其对应的每个单词都要加入到p.word中,没有条件,不理解
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 当前字母对应的节点p.next[c - 'a'];非空时,说明trie中已经存了和图中相对应的节点,p就应该指向当前对应的字母节点,否则新建。没有理解
[复杂度]:Time complexity: O(4*深度次方,最大m+n) Space complexity: O(<n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 新建的一棵trie树,按道理应该习惯命名为root
- 可以有几个类,但是只能有一个public的类 其他的你就只能定义class而已
- 当 java 一个 class 里面 包含 另一个 class 时,需要将这个子 class 声明为 static,不然经常出错
[关键模板化代码]:
for (String w : words){
TrieNode p = root;
//add a c
for (char c : w.toCharArray()) {
int i = c - 'a';
if (p.next[i] == null) {
p.next[i] = new TrieNode();
}
//asign!
p = p.next[i];
}
//add word
p.word = w;//only mean to w, sure to have word
}
指针开始指向root,之后指向别的
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
word search1 : dfs
[代码风格] :
j的for循环写成i了,查了半小时,后悔死。好像老是出现这个错误吧?要避免。
class Solution {
//class
class TrieNode {
TrieNode[] next = new TrieNode[26];
String word;
}
public List<String> findWords(char[][] board, String[] words) {
List<String> res = new ArrayList<String>();
TrieNode root = buildTrie(words);
for (int i = 0; i < board.length; i++) {
for (int j = 0; i < board[0].length; j++) {
dfs(board, i, j, root, res);
}
}
return res;
}
//dfs
public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
//exit
//no c or no node
char c = board[i][j];
if (c == '#' || p.next[c - 'a'] == null) {//exit in dfs
return ;
}
//asign
p = p.next[c - 'a'];
//add word
if (p.word != null) {
res.add(p.word);
p.word = null;
}
//dfs
board[i][j] = '#';
if (i < board.length - 1) dfs(board, i + 1, j, p, res);
if (j < board[0].length - 1) dfs(board, i, j + 1, p, res);
if (i > 0) dfs(board, i - 1, j, p, res);
if (j > 0) dfs(board, i, j - 1, p, res);
board[i][j] = c;
}
//buildTrie
public TrieNode buildTrie (String[] words) {
//build a root
TrieNode root = new TrieNode();
//add all words's node
for (String w : words){
TrieNode p = root;
//add a c
for (char c : w.toCharArray()) {
int i = c - 'a';
if (p.next[i] == null) {
p.next[i] = new TrieNode();
}
}
//add word
if (p.word != null) {
res.add(p.word);
}
}
return root;
}
}
单词搜索 II · Word Search II的更多相关文章
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- [Swift]LeetCode212. 单词搜索 II | Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- LeetCode 79. 单词搜索(Word Search)
题目描述 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被 ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] 212. Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
随机推荐
- keras_基本网络层结构(1)_常用层
参考文献: https://blog.csdn.net/sinat_26917383/article/details/72857454 http://keras-cn.readthedocs.io/e ...
- git 基础学习笔记
配置ssh 打开命令行或者git bash 输入 创建git库 git init 检查当前状态 $ git status On branch master nothing to commit, wor ...
- 0基础小白怎么学好Java?
自身零基础,我们应该先学好Java,小编给大家介绍一下Java的特性: Java语言是简单的 Java语言的语法与C语言和C++语言很接近,使得大多数程序员很容易学习和使用Java.Java丢弃了C+ ...
- 这是C语言结课前(期末考试之前)写给牛晓霞的一封信!
致尊敬的牛晓霞老师: 这是黄领衫的感想,也是想告诉你的话! 在老师说要给班里写得好的人发黄领衫的时候,我当时的想法是我很有可能拿到这份奖品的,怎么说呢,算是一种自信吧,或是对自己的态度的认可.虽然我能 ...
- # 20155327 2016-2017-4 《Java程序设计》第8周学习总结
20155327 2016-2017-4 <Java程序设计>第7周学习总结 教材学习内容总结 了解NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你设定 ...
- test20181024 ming
题意 分析 考场做法 考虑二分答案,R开到1e9就能过了. 判断答案合法,就判断时间和是否超过拥有的时间就行了.但要把di从小到大排序,不然容易验证贪心是错的. 时间复杂度\(O(n \log n)\ ...
- Linux下sniffer实现(转)
转发网址:https://blog.csdn.net/eqiang8271/article/details/8489769 //Example 1. #include <stdio.h> ...
- 打造html右键菜单
今天是给大家介绍一款在网页上使用的右键菜单,原作者的网址是:http://51jsr.javaeye.com/blog/305517 这个右键菜单已经非常优秀,不过呢.却是IE Only,而且在DTD ...
- 图解VS2005之单元测试
据说VS2005里即提供了测试功能,可是对于像我或者我们这样的开发人或团队真还没有进化到用测试这块.一直以来都是手工测试或等到用户发现问题.今天在网上找了一个介绍单元测试的WORD文档,按里面说的做了 ...
- Phonegap 通信原理
下图为JavaScript调用本地代码的通信过程 Phonegap的核心API都是基于插件的,这些JavaScript API都会调用cordova.exec() 函数来完成操作.cordova.ex ...