关于leetcode:leetcode-208-Implement-Trie-Prefix-Tree-实现-Trie-前缀树-中等

2次阅读

共计 2262 个字符,预计需要花费 6 分钟才能阅读完成。

一、题目粗心

Trie(发音相似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的利用情景,例如主动补完和拼写查看。

请你实现 Trie 类:

  • Trie() 初始化前缀树对象。
  • void insert(String word) 向前缀树中插入字符串 word。
  • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前曾经插入);否则,返回 false。
  • boolean startsWith(String prefix) 如果之前曾经插入的字符串 word 的前缀之一为 prefix,返回 true;否则,返回 false。

示例:

 输出
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输入
[null, null, true, false, true, null, true]

解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // 返回 True
trie.search("app");     // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app");     // 返回 True

提醒:

  • 1 <= word.length, prefix.length <= 2000
  • word 和 prefix 仅由小写英文字母组成
  • insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次

起源:力扣(LeetCode)
链接:https://leetcode.cn/problems/…
著作权归领扣网络所有。商业转载请分割官网受权,非商业转载请注明出处。

二、解题思路

这道题让咱们实现一个数据结构 - 字典树,又称前缀树或单词查找树。

字典树次要有如下三个性质:

1、根节点不蕴含字符,除根节点外每个节点只蕴含一个字符

2、从根节点到某一个节点,门路上通过的字符连接起来,为该节点对应的字符串

3、每个节点的所有子节点蕴含的字符串不雷同

字典树的插入、删除和查找都不难,用一个一重循环即可,即第 i 次循环找到前 i 个字母所对应的子树,而后进行相应的操作。实现这棵字母 树,能够用最常见的数组保留即可(动态开拓内存)。至于节点对儿子的指向,个别有三种办法:

1、对每个节点开一个字母集大小的数组,对应的下标是儿子所示意的字母,内容则是这个儿子对应在大数组上的地位,即标号

2、对每个节点挂一个链表,按肯定程序记录每个儿子是谁

3、应用左儿子右兄弟表示法记录这棵树

第一种:易实现但理论空间要求较大

第二种:易实现空间要求较小但比拟费时

第三种:空间要求最小但绝对费时且不易写

咱们用第一种办法实现,字典树的每个节点要定义一个大小为 26 字节的 int 数组,而后用一个标志符来记录到以后地位为止是否为一个词,初始化的时候将 26 个节点都赋为空。那么 insert 操作只须要对要插入的字符串的每个字符计算出其地位,而后找是否存在在这个节点,若不存在则新建一个,而后再查找下一个。查找词和找前缀操作跟 insert 操作很相似,不同点在于若不存在子节点则返回 false。查找到最初还要看标识位,而找前缀间接返回 true 即可。

三、解题办法

3.1 Java 实现

class TrieNode {TrieNode[] child;
   boolean isWord;

   TrieNode() {
       this.isWord = false;
       child = new TrieNode[26];
   }
}

class Trie {

    private TrieNode root;

    public Trie() {root = new TrieNode();
    }

    public void insert(String word) {
        TrieNode p = root;
        for (char c : word.toCharArray()) {
            int i = c - 'a';
            if (p.child[i] == null) {p.child[i] = new TrieNode();}
            p = p.child[i];
        }
        p.isWord = true;
    }

    public boolean search(String word) {
        TrieNode p = root;
        for (char c : word.toCharArray()) {
            int i = c - 'a';
            if (p.child[i] == null) {return false;}
            p = p.child[i];
        }
        return p.isWord;
    }

    public boolean startsWith(String prefix) {
        TrieNode p = root;
        for (char c : prefix.toCharArray()) {
            int i = c - 'a';
            if (p.child[i] == null) {return false;}
            p = p.child[i];
        }
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

四、总结小记

  • 2022/9/26 养成一个小习惯而后保持下来
正文完
 0