zoukankan      html  css  js  c++  java
  • 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);
     */
  • 相关阅读:
    工业4G DTU是什么和普通DTU有什么不同
    NB-IOT基站的优势和特点
    rs485通讯模块有什么作用
    串口服务器的具体做用
    4G DTU是什么 可以应用于哪些行业?
    关于linux文件出现属性显示?????????? ? ?问题的一些解决方法
    log4j.properties配置与将异常输出到Log日志文件实例
    将字符串向hdfs中写入,出现中文乱码!
    impala操作hase、hive
    Kudu基本操作及概念
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6632455.html
Copyright © 2011-2022 走看看