442. Implement Trie (Prefix Tree)

  1. class TrieNode {
  2. public boolean isWord;
  3. public TrieNode[] children;
  4.  
  5. public TrieNode() {
  6. isWord = false;
  7. children = new TrieNode[26];
  8. }
  9.  
  10. }
  11.  
  12. public class Trie {
  13. private TrieNode root;
  14.  
  15. public Trie() {
  16. // do intialization if necessary
  17. root = new TrieNode();
  18. }
  19.  
  20. /*
  21. * @param word: a word
  22. * @return: nothing
  23. */
  24. public void insert(String word) {
  25. // write your code here
  26. if (word == null || word.length() == 0) {
  27. return;
  28. }
  29. TrieNode p = root;
  30. for (int i = 0; i < word.length(); i++) {
  31. int index = word.charAt(i) - 'a';
  32. if (p.children[index] == null) {
  33. p.children[index] = new TrieNode();
  34. }
  35. p = p.children[index];
  36. }
  37. p.isWord = true;
  38. }
  39.  
  40. public TrieNode find(String prefix) {
  41. if (prefix == null || prefix.length() == 0) {
  42. return null;
  43. }
  44. TrieNode p = root;
  45. for (int i = 0; i < prefix.length(); i++) {
  46. int index = prefix.charAt(i) - 'a';
  47. if (p.children[index] == null) {
  48. return null;
  49. }
  50. p = p.children[index];
  51. }
  52. return p;
  53. }
  54.  
  55. /*
  56. * @param word: A string
  57. * @return: if the word is in the trie.
  58. */
  59. public boolean search(String word) {
  60. // write your code here
  61. TrieNode p = find(word);
  62. return p != null && p.isWord;
  63. }
  64.  
  65. /*
  66. * @param prefix: A string
  67. * @return: if there is any word in the trie that starts with the given prefix.
  68. */
  69. public boolean startsWith(String prefix) {
  70. // write your code here
  71. return find(prefix) != null;
  72. }
  73. }

473. Add and Search Word - Data structure design

  1. class TrieNode {
  2. public boolean isWord;
  3. public char c;
  4. public Map<Character, TrieNode> children;
  5.  
  6. public TrieNode() {
  7. children = new HashMap<>();
  8. }
  9.  
  10. public TrieNode(char c) {
  11. this.c = c;
  12. children = new HashMap<>();
  13. }
  14. }
  15.  
  16. public class WordDictionary {
  17. /*
  18. * @param word: Adds a word into the data structure.
  19. * @return: nothing
  20. */
  21. TrieNode root = new TrieNode();
  22.  
  23. public void addWord(String word) {
  24. // write your code here
  25. if (word == null || word.length() == 0) {
  26. return;
  27. }
  28.  
  29. TrieNode p = root;
  30. for (int i = 0; i < word.length(); i++) {
  31. char c = word.charAt(i);
  32. if (p.children.get(c) == null) {
  33. TrieNode child = new TrieNode();
  34. p.children.put(c, child);
  35. }
  36.  
  37. p = p.children.get(c);
  38. }
  39. p.isWord = true;
  40. }
  41.  
  42. /*
  43. * @param word: A word could contain the dot character '.' to represent any one letter.
  44. * @return: if the word is in the data structure.
  45. */
  46. public boolean search(String word) {
  47. // write your code here
  48. if (word == null || word.length() == 0) {
  49. return false;
  50. }
  51. TrieNode p = root;
  52.  
  53. for (int i = 0; i < word.length(); i++) {
  54. char c = word.charAt(i);
  55. if (c != '.') {
  56. if (p.children.get(c) == null) {
  57. return false;
  58. }
  59. p = p.children.get(c);
  60. } else {
  61. for (Map.Entry<Character, TrieNode> entry : p.children.entrySet()) {
  62. if (search(word.substring(0, i) + entry.getKey() + word.substring(i + 1, word.length()))) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. }
  70.  
  71. return p.isWord;
  72. }
  73. }

132. Word Search II

  1. class TrieNode {
  2. String word;
  3. Map<Character, TrieNode> children;
  4.  
  5. public TrieNode() {
  6. children = new HashMap<>();
  7. }
  8. }
  9.  
  10. class TrieTree {
  11. TrieNode root;
  12.  
  13. public TrieTree(TrieNode node) {
  14. this.root = node;
  15. }
  16.  
  17. public void insert(String word) {
  18. if (word == null || word.length() == 0) {
  19. return;
  20. }
  21. TrieNode p = root;
  22. for (int i = 0; i < word.length(); i++) {
  23. char ch = word.charAt(i);
  24. if (!p.children.containsKey(ch)) {
  25. p.children.put(ch, new TrieNode());
  26. }
  27. p = p.children.get(ch);
  28. }
  29. p.word = word;
  30. }
  31. }
  32.  
  33. public class Solution {
  34. private int[] dx = {0, 1, 0, -1};
  35. private int[] dy = {1, 0, -1, 0};
  36.  
  37. /**
  38. * @param board: A list of lists of character
  39. * @param words: A list of string
  40. * @return: A list of string
  41. */
  42. public List<String> wordSearchII(char[][] board, List<String> words) {
  43. // write your code here
  44. if (words == null || words.size() == 0) {
  45. return new ArrayList<>();
  46. }
  47. Set<String> res = new HashSet<>();
  48. TrieTree tree = new TrieTree(new TrieNode());
  49. for (String word : words) {
  50. tree.insert(word);
  51. }
  52. for (int i = 0; i < board.length; i++) {
  53. for (int j = 0; j < board[i].length; j++) {
  54. dfs(board, i, j, res, tree.root);
  55. }
  56. }
  57. return new ArrayList<>(res);
  58. }
  59.  
  60. public void dfs(char[][] board, int x, int y, Set<String> res, TrieNode node) {
  61. TrieNode child = node.children.get(board[x][y]);
  62. if (child == null) {
  63. return;
  64. }
  65. if (child.word != null) {
  66. if (!res.contains(child.word)) {
  67. res.add(child.word);
  68. }
  69. }
  70.  
  71. char tmp = board[x][y];
  72. board[x][y] = 0;
  73. for (int i = 0; i < dx.length; i++) {
  74. int nxtDx = x + dx[i];
  75. int nxtDy = y + dy[i];
  76. if (!isValid(board, nxtDx, nxtDy)) {
  77. continue;
  78. }
  79. dfs(board, nxtDx, nxtDy, res, child);
  80. }
  81. board[x][y] = tmp;
  82. }
  83.  
  84. public boolean isValid(char[][] board, int x, int y) {
  85. if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) {
  86. return false;
  87. }
  88. return board[x][y] != 0;
  89. }
  90. }

Trie - 20181113的更多相关文章

  1. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  2. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  9. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

随机推荐

  1. 使用 Sentry集中处理错误

    Sentry的简介 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将程序的所有 exception 自动记录下来,处理 exception 是每个程 ...

  2. Celery笔记

    异步任务神器 Celery 简明笔记 2016/12/19 · 工具与框架 · Celery, 异步 原文出处: FunHacks    在程序的运行过程中,我们经常会碰到一些耗时耗资源的操作,为了避 ...

  3. 跨域Ajax请求(jQuery JSONP MVC)

    通过jQuery的$.ajax方法发送JSONP请求 js代码 <script type="text/javascript"> function jsonptest2( ...

  4. OM—>AR相关会计科目

    业务会计核算 挑库:           借:发出商品 (递延销货成本) 贷:发出商品 (递延销货成本)   发运:           借:发出商品 (递延销货成本) 贷:库存商品/原材料 (库存估 ...

  5. 将“100px” 转换为100

    parseInt("100px") //结果是100

  6. fabric Clone

    记录下: var newObj = fabric.util.object.clone(obj); decDoc.dropCanvas.add(newObj., top: }));

  7. Log--检查各数据库日志的使用情况

    -- Recovery model, log reuse wait description, log file size,-- log usage size and compatibility lev ...

  8. webform Response的一些成员

    1. Response.BufferOutPut,关闭缓冲区. 2. Response.Flush,一次性把缓冲区的内容释放出来. 3. Response.Clear,清空缓冲区. 4. Respon ...

  9. 基于CentOS6定制自己的ISO安装光盘

    警告:转载请注明出处 https://www.cnblogs.com/BoyTNT/p/9322927.html  1.目标 >> 基于CentOS-6.10-x86_64-minimal ...

  10. Mysql避免重复插入记录方法

    一.mysql replace用法 1.replace into  replace into table (id,name) values('1','aa'),('2','bb')  此语句的作用是向 ...