zoukankan      html  css  js  c++  java
  • <Trie> 208 211

    208. Implement Trie (Prefix Tree)

    class TrieNode{
        private char val;
        public boolean isWord;
        public TrieNode[] children = new TrieNode[26];
        public TrieNode(){};
        public TrieNode(char c){
            this.val = c;
        }
    }
    class Trie {
        private TrieNode root;
    
        /** Initialize your data structure here. */
        public Trie() {
            root = new TrieNode(' ');
        }
        
        /** Inserts a word into the trie. */
        public void insert(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); i++){
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null){
                    cur.children[c - 'a'] = new TrieNode(c);
                }
                cur = cur.children[c - 'a'];
            }
            cur.isWord = true;
        }
        
        /** Returns if the word is in the trie. */
        public boolean search(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); i++){
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return cur.isWord;
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public boolean startsWith(String prefix) {
            TrieNode cur = root;
            for(int i = 0; i < prefix.length(); i++){
                char c =prefix.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return true;
        }
    }

    211. Add and Search Word - Data structure design

    class WordDictionary {
        
        public class TrieNode{
            public TrieNode[] children = new TrieNode[26];
            public boolean isWord;
        }
        
        private TrieNode root = new TrieNode();
        /** Initialize your data structure here. */
        public WordDictionary() {
            
        }
        
        /** Adds a word into the data structure. */
        public void addWord(String word) {
            TrieNode node = root;
            for(char c : word.toCharArray()){
                if(node.children[c - 'a'] == null){
                    node.children[c - 'a'] = new TrieNode();
                }
                node = node.children[c - 'a'];
            }
            node.isWord = true;
        }
        
        /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
        public boolean search(String word) {
            return match(word.toCharArray(), 0, root);
        }
        
        private boolean match(char[] chs, int start, TrieNode node){
            if(start == chs.length) return node.isWord;
            
            if(chs[start] == '.'){
                for(int i = 0; i < node.children.length; i++){
                    if(node.children[i] != null && match(chs, start + 1, node.children[i]))
                        return true;
                } 
            }else{
                return node.children[chs[start] - 'a'] != null && match(chs, start + 1, node.children[chs[start] - 'a']);
            }
        return false;
        }
    }
  • 相关阅读:
    java学习笔记07-循环
    java学习笔记06-条件语句
    java学习笔记05-运算符
    java学习笔记04-基本数据类型
    java学习笔记03-基本语法
    java学习笔记02-Eclipse IDE配置
    java学习笔记01-环境配置
    Jmeter学习笔记03-元件作用域及执行顺序
    JMeter学习笔记02-基础介绍
    [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
  • 原文地址:https://www.cnblogs.com/Afei-1123/p/11862829.html
Copyright © 2011-2022 走看看