【Hot100】LeetCode—208. 实现 Trie (前缀树)



1- 思路

数组哈希表 + isEnd 字段

前缀树的当前结点,存储的是下一个子节点的元素值。当前结点中的内容是一个哈希表,和 isEnd 字段
① 前缀树节点

class TrieNode{
    boolean isEnd = false;
    TrieNode[] children;
    public TriNode(){
        children = new TrieNode[26];
    }
}

② 插入操作

  • 2.1 获取 root 结点赋值为 cur
  • 2.2 遍历 word 进行哈希,创造子节点的操作
  • 2.3 插入完移动 cur 指针
  • 2.4 最后赋值 isEnd

③ search操作

  • 与插入代码类似,区别点在于 遍历的过程:如果遇到 null 直接返回 false
  • 最终返回的是 isEnd

④ startWith 操作

  • 和 search 操作的区别在于,返回值

2- 实现

⭐208. 实现 Trie (前缀树)——题解思路

在这里插入图片描述

class Trie {

    class TrieNode{
        TrieNode[] children;
        boolean isEnd;
        public TrieNode(){
            children = new TrieNode[26];
            isEnd = false;
        }
    }

    TrieNode root;
    public Trie() {
        root = new TrieNode();
    }
    
    public void insert(String word) {
        // 遍历word
        TrieNode cur = root;
        for(int i = 0 ; i < word.length();i++){
            char c = word.charAt(i);
            int index = c-'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode();
            }
            cur = cur.children[index];
        }
        cur.isEnd = true;
    }
    
    public boolean search(String word) {
        // 遍历word
        TrieNode cur = root;
        for(int i = 0 ; i < word.length();i++){
            char c = word.charAt(i);
            int index = c-'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        return cur.isEnd;
    }
    
    public boolean startsWith(String prefix) {
        // 遍历word
        TrieNode cur = root;
        for(int i = 0 ; i < prefix.length();i++){
            char c = prefix.charAt(i);
            int index = c-'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        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);
 */

3- ACM 实现

class Trie {

    class TrieNode {
        TrieNode[] children;
        boolean isEnd;

        public TrieNode() {
            children = new TrieNode[26];
            isEnd = false;
        }
    }

    TrieNode root;

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

    public void insert(String word) {
        TrieNode cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            int index = c - 'a';
            if (cur.children[index] == null) {
                cur.children[index] = new TrieNode();
            }
            cur = cur.children[index];
        }
        cur.isEnd = true;
    }

    public boolean search(String word) {
        TrieNode cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            int index = c - 'a';
            if (cur.children[index] == null) {
                return false;
            }
            cur = cur.children[index];
        }
        return cur.isEnd;
    }

    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for (int i = 0; i < prefix.length(); i++) {
            char c = prefix.charAt(i);
            int index = c - 'a';
            if (cur.children[index] == null) {
                return false;
            }
            cur = cur.children[index];
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Trie trie = new Trie();
        while (scanner.hasNext()) {
            String command = scanner.next();
            if (command.equals("insert")) {
                String word = scanner.next();
                trie.insert(word);
            } else if (command.equals("search")) {
                String word = scanner.next();
                System.out.println(trie.search(word));
            } else if (command.equals("startsWith")) {
                String prefix = scanner.next();
                System.out.println(trie.startsWith(prefix));
            }
        }
        scanner.close();
    }
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值