1. //Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼
    2. //写检查。
    3. //
    4. // 请你实现 Trie 类:
    5. //
    6. //
    7. // Trie() 初始化前缀树对象。
    8. // void insert(String word) 向前缀树中插入字符串 word 。
    9. // boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false
    10. // 。
    11. // boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否
    12. //则,返回 false 。
    13. //
    14. //
    15. //
    16. //
    17. // 示例:
    18. //
    19. //
    20. //输入
    21. //["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
    22. //[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
    23. //输出
    24. //[null, null, true, false, true, null, true]
    25. //
    26. //解释
    27. //Trie trie = new Trie();
    28. //trie.insert("apple");
    29. //trie.search("apple"); // 返回 True
    30. //trie.search("app"); // 返回 False
    31. //trie.startsWith("app"); // 返回 True
    32. //trie.insert("app");
    33. //trie.search("app"); // 返回 True
    34. //
    35. //
    36. //
    37. //
    38. // 提示:
    39. //
    40. //
    41. // 1 <= word.length, prefix.length <= 2000
    42. // word 和 prefix 仅由小写英文字母组成
    43. // insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
    44. //
    45. // Related Topics 设计 字典树
    46. // 👍 639 👎 0

    字典树用来快速判断字符集固定的字符范围挺实用,是一道模板题

    1. class Trie {
    2. class Node{
    3. char ch;
    4. // 标志下一层节点,初始都为null
    5. Node[] next = new Node[26];
    6. // 是否有单词结束在该节点
    7. boolean end = false;
    8. }
    9. Node root;
    10. /** Initialize your data structure here. */
    11. public Trie() {
    12. root = new Node();
    13. }
    14. /** Inserts a word into the trie. */
    15. public void insert(String word) {
    16. Node cur = root;
    17. for (int i = 0; i < word.length(); i++) {
    18. char ch = word.charAt(i);
    19. // 如果下一个节点是空,就新建一个节点
    20. if (cur.next[ch - 'a'] == null) {
    21. cur.next[ch - 'a'] = new Node();
    22. cur.next[ch - 'a'].ch = ch;
    23. }
    24. // 将cur指针指向下一个节点
    25. cur = cur.next[ch - 'a'];
    26. }
    27. // 单词结束在cur节点上,将end置为true
    28. cur.end = true;
    29. }
    30. /** Returns if the word is in the trie. */
    31. public boolean search(String word) {
    32. Node cur = root;
    33. for (int i = 0; i < word.length(); i++) {
    34. char ch = word.charAt(i);
    35. // 如果下一个节点为null,直接返回false,说明没有添加过这个字符串
    36. if (cur.next[ch - 'a'] == null) {
    37. return false;
    38. }
    39. cur = cur.next[ch - 'a'];
    40. }
    41. // 比如添加了apple, 搜索app的时候,需要判读是否该节点有结束的字符串
    42. return cur.end;
    43. }
    44. /** Returns if there is any word in the trie that starts with the given prefix. */
    45. public boolean startsWith(String prefix) {
    46. Node cur = root;
    47. for (int i = 0; i < prefix.length(); i++) {
    48. char ch = prefix.charAt(i);
    49. if (cur.next[ch - 'a'] == null) {
    50. return false;
    51. }
    52. cur = cur.next[ch - 'a'];
    53. }
    54. // 只找前缀就不用判断是否有结束了
    55. return true;
    56. }
    57. }