[抄题]:

给出一个由小写字母组成的矩阵和一个字典。找出所有同时在字典和矩阵中出现的单词。一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动。

给出矩阵:

  1. doaf
    agai
    dcan

和字典:

  1. {"dog", "dad", "dgdg", "can", "again"}
 
返回 {"dog", "dad", "can", "again"}

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 用p指针进行比较,不知道指针应该怎么用,指针就是一个新类,利用其中的TrieNode[] next数组移动即可。表示节点的初始化,
    1. p = p.next[c - 'a'];使节点等于初始化之后的值。
  2. 为防止出现res中有相同单词,每添加一个单词后就设其为空, 不同路线相同结果时要想到

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

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

建树时,每个单词都用p从root开始重新新建

[一刷]:

  1. dfs的参数在总函数中有用的变量一一对应就行了,数组名还是要有的
  2. next[],忘记了 数组括号中一定只能是数字,不能是别的,应该是c - 'a'
  3. board[i][j]直接= 字母c就行了,不用加‘’
  4. 纠结不清的:dfs的表达式上限是由总表达式的上限决定的,
    1. i < board.length, dfs(i+1)也要带入后符合规则,故
    1. i < board.length - 1

[二刷]:

  1. 当前字母对应的节点p.next[c - 'a'];非空时,说明trie中已经存了和图中相对应的节点,p就应该指向当前对应的字母节点,否则新建。没有理解
  2. w循环时,其对应的每个单词都要加入到p.word中,没有条件,不理解

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 当前字母对应的节点p.next[c - 'a'];非空时,说明trie中已经存了和图中相对应的节点,p就应该指向当前对应的字母节点,否则新建。没有理解

[复杂度]:Time complexity: O(4*深度次方,最大m+n) Space complexity: O(<n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 新建的一棵trie树,按道理应该习惯命名为root
  2. 可以有几个类,但是只能有一个public的类 其他的你就只能定义class而已
  3. 当 java 一个 class 里面 包含 另一个 class 时,需要将这个子 class 声明为 static,不然经常出错

[关键模板化代码]:

  1. for (String w : words){
  2. TrieNode p = root;
  3. //add a c
  4. for (char c : w.toCharArray()) {
  5. int i = c - 'a';
  6. if (p.next[i] == null) {
  7. p.next[i] = new TrieNode();
  8. }
  9. //asign!
  10. p = p.next[i];
  11. }
  12. //add word
  13. p.word = w;//only mean to w, sure to have word
  14. }

指针开始指向root,之后指向别的

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

word search1 : dfs

[代码风格] :

j的for循环写成i了,查了半小时,后悔死。好像老是出现这个错误吧?要避免。

  1. class Solution {
  2. //class
  3. class TrieNode {
  4. TrieNode[] next = new TrieNode[26];
  5. String word;
  6. }
  7.  
  8. public List<String> findWords(char[][] board, String[] words) {
  9. List<String> res = new ArrayList<String>();
  10. TrieNode root = buildTrie(words);
  11.  
  12. for (int i = 0; i < board.length; i++) {
  13. for (int j = 0; i < board[0].length; j++) {
  14. dfs(board, i, j, root, res);
  15. }
  16. }
  17. return res;
  18. }
  19. //dfs
  20. public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
  21. //exit
  22. //no c or no node
  23. char c = board[i][j];
  24. if (c == '#' || p.next[c - 'a'] == null) {//exit in dfs
  25. return ;
  26. }
  27. //asign
  28. p = p.next[c - 'a'];
  29. //add word
  30. if (p.word != null) {
  31. res.add(p.word);
  32. p.word = null;
  33. }
  34. //dfs
  35. board[i][j] = '#';
  36. if (i < board.length - 1) dfs(board, i + 1, j, p, res);
  37. if (j < board[0].length - 1) dfs(board, i, j + 1, p, res);
  38. if (i > 0) dfs(board, i - 1, j, p, res);
  39. if (j > 0) dfs(board, i, j - 1, p, res);
  40. board[i][j] = c;
  41. }
  42. //buildTrie
  43. public TrieNode buildTrie (String[] words) {
  44. //build a root
  45. TrieNode root = new TrieNode();
  46. //add all words's node
  47. for (String w : words){
  48. TrieNode p = root;
  49. //add a c
  50. for (char c : w.toCharArray()) {
  51. int i = c - 'a';
  52. if (p.next[i] == null) {
  53. p.next[i] = new TrieNode();
  54. }
  55. }
  56. //add word
  57. if (p.word != null) {
  58. res.add(p.word);
  59. }
  60. }
  61. return root;
  62. }
  63. }

单词搜索 II · Word Search II的更多相关文章

  1. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

  2. [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 ...

  3. LeetCode 79. 单词搜索(Word Search)

    题目描述 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被 ...

  4. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

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

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

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. 第8课 goto和void分析

    遭人遗弃的goto: C语言是一种面向过程的结构化语言,其中主要结构有三种,顺序执行.选择执行.循环执行.再复杂的程序也是由这三种结构组合而成的. goto破坏了结构化特性,使程序以第四种方式执行,结 ...

  2. Python数据类型-01.数字和布尔值

    本节主要介绍Python中的基础知识中的数据类型,数字和布尔值 介绍几个知识点:1)内置函数print()的用法,直接打印括号里面的内容,或者print后跟多个输出,以逗号分隔.2)内置函数type( ...

  3. BZOJ3211: 花神游历各国(线段树)

    3211: 花神游历各国 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 5692  Solved: 2114[Submit][Status][Discu ...

  4. SpringBoot实现网站注册,邮件激活码激活功能

    项目源码:https://gitee.com/smfx1314/springbootemail 上一篇文章已经讲到如何springboot如何实现邮件的发送,趁热打铁,这篇文章实现如下功能. 很多网站 ...

  5. rabbitmq学习(三):rabbitmq之扇形交换机、主题交换机

    前言 上篇我们学习了rabbitmq的作用以及直连交换机的代码实现,这篇我们继续看如何用代码实现扇形交换机和主题交换机 一.扇形交换机 1.生产者 /** * 生产者 */ public class ...

  6. 日志异常:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.StaticLoggerBinder

    今天启动开发的项目,碰到了一个日志上的bug:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.Sta ...

  7. C#:进程、线程、应用程序域(AppDomain)与上下文分析

    进程     进程是操作系统用于隔离众多正在运行的应用程序的机制.在.Net之前,每一个应用程序被加载到单独的进程中,并为该进程指定私有的虚拟内存.进程不能直接访问物理内存,操作系统通过其它的处理把这 ...

  8. VS2017新建windows控制台程序打印中文乱码问题

    最近刚换上VS2017,由于手头又要做个MFC的程序,所以写控制台程序做功能测试,然后发现居然乱码了. 于是用VS2017新建windows控制台应用程序,在main函数种加一句printf(&quo ...

  9. 启动zookeeper时,jps显示有进程,但是status查看状态时就Error contacting service. It is probably not running

    转自:http://www.cnblogs.com/xiaohua92/p/5460515.html#undefined 安装zookeeper时候,可以查看进程启动,但是状态显示报错:Error c ...

  10. 关于web api 中 日期格式问题解决方案

    在构造函数或者 全局开始的时候调用这个 public BossApiController() { JsonMediaTypeFormatter jsonFormatter = GlobalConfig ...