LeetCode Top 100 Liked Questions 208. Implement Trie (Prefix Tree) (Java版; Medium)

welcome to my blog

LeetCode Top 100 Liked Questions 208. Implement Trie (Prefix Tree) (Java版; Medium)

题目描述
Implement a trie with insert, search, and startsWith methods.

Example:

Trie trie = new Trie();

trie.insert("apple");
trie.search("apple");   // returns true
trie.search("app");     // returns false
trie.startsWith("app"); // returns true
trie.insert("app");   
trie.search("app");     // returns true
Note:

You may assume that all inputs are consist of lowercase letters a-z.
All inputs are guaranteed to be non-empty strings.
class Trie {
    
    Node root;
    /** Initialize your data structure here. */
    public Trie() {
        root = new Node();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        if(word==null){
            return;
        }
        char[] chs = word.toCharArray();
        Node cur = root;
        cur.pass++;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(cur.nexts[index]==null){
                cur.nexts[index] = new Node();
            }
            cur = cur.nexts[index];
            cur.pass++;
        }
        cur.end++;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        if(word==null || word.equals("")){
            return false;
        }
        char[] chs = word.toCharArray();
        Node cur = root;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(cur.nexts[index]==null){
                return false;
            }
            cur = cur.nexts[index];
        }
        return cur.end>0;

    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        if(prefix==null || prefix.equals("")){
            return false;
        }
        char[] chs = prefix.toCharArray();
        Node cur = root;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(cur.nexts[index]==null){
                return false;
            }
            cur = cur.nexts[index];
        }
        return cur.pass>0;
    }

    private class Node{
        int pass;
        int end;
        Node[] nexts;
        Node(){
            nexts = new Node[26];
        }
    }
}
第一次做; 前缀树; 前缀树的各个函数都很像, 比如前四行都一样, for循环条件都一样, 里面的内容有比较像; 一切都要从根节点开始; curr.nexts[index]指的是当前这个char, 不是下一个char; 基础, 要夯实!
/*
先定义前缀树节点
*/
class TrieNode{
    int pass;
    int end;
    TrieNode[] nexts;
    TrieNode(){
        nexts = new TrieNode[26];
    }
}
/*
接着实现前缀树
*/
class Trie {
    //一切要从根节点开始
    TrieNode root;
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        //input check(也不用写, 因为题目保证了输入非空的string)
        if(word==null)
            return;
        char[] chs = word.toCharArray();
        TrieNode curr = root;
        curr.pass++;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(curr.nexts[index]==null)
                curr.nexts[index] = new TrieNode();
            curr = curr.nexts[index];
            curr.pass++;
        }
        //路径最后一个节点的end要++
        curr.end++;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        if(word==null)
            return false;
        char[] chs = word.toCharArray();
        TrieNode curr = root;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(curr.nexts[index] == null)
                return false;
            curr = curr.nexts[index];
        }
        return curr.end>0;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        if(prefix==null)
            return false;
        char[] chs = prefix.toCharArray();
        TrieNode curr = root;
        for(int i=0; i<chs.length; i++){
            int index = chs[i] - 'a';
            if(curr.nexts[index]==null)
                return false;
            curr = curr.nexts[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);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值