• leetcode 208. Implement Trie (Prefix Tree) 设计前缀树 ---------- java


    Implement a trie with insertsearch, and startsWith methods.

    Note:
    You may assume that all inputs are consist of lowercase letters a-z.

    设计一个前缀树,包括insert,search,startsWith三种方法。
     
    1、每个字母用一个Trie表示,同时isWord表示到目前Trie是否构成一个单词。
    public class Trie {
    
    
        public char ch;
        public Trie[] nextTrie;
        public boolean isWord;
        
        /** Initialize your data structure here. */
        public Trie() {
            nextTrie = new Trie[26];
        }
        public Trie(char ch){
            this.ch = ch;
            nextTrie = new Trie[26];
    
        }
        
        /** Inserts a word into the trie. */
        public void insert(String word) {
            Trie[] nextTrie = this.nextTrie;
            for (int i = 0; i < word.length(); i++){
                int num = (int) (word.charAt(i) - 'a');
                if (nextTrie[num] == null){
                    nextTrie[num] = new Trie(word.charAt(i));
                }
                if (i == word.length() - 1){
                    nextTrie[num].isWord = true;
                }
                nextTrie = nextTrie[num].nextTrie;
            }
        }
        
        /** Returns if the word is in the trie. */
        public boolean search(String word) {
            
            Trie[] nextTrie = this.nextTrie;
            for (int i = 0; i < word.length() - 1; i++){
                int num = (int) (word.charAt(i) - 'a');
                if (nextTrie[num] == null){
                    return false;
                }
                nextTrie = nextTrie[num].nextTrie;
            }
            int num = (int) (word.charAt(word.length() - 1) - 'a');
            if (nextTrie[num] == null || nextTrie[num].isWord == false){
                return false;
            } else {
                return true;
            }
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public boolean startsWith(String word) {
            Trie[] nextTrie = this.nextTrie;
            for (int i = 0; i < word.length(); i++){
                int num = (int) (word.charAt(i) - 'a');
                if (nextTrie[num] == null){
                    return false;
                }
                nextTrie = nextTrie[num].nextTrie;
            }
            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);
     */
  • 相关阅读:
    创建型-单例模式(Singleton)
    创建型-抽象工厂模式(Abstract Factory)
    创建型-工厂模式(Factory Method)
    excel数据查找/匹配/合并--vlookup公式使用
    【分库分表/读写分离】学习+整理
    java中main方法的参数的作用
    JVM指令码手册
    bug:Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded
    sql:基础总结
    转:【host文件作用】
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6632455.html
Copyright © 2020-2023  润新知