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;
        }
    }
  • 相关阅读:
    NetScaler ‘Counters’ Grab-Bag!
    NetScaler + Wireshark = A Perfect Combination!
    What’s That NetScaler Reset Packet?
    Give NetScaler a “Tune-Up”
    51Nod 1080 两个数的平方和(数论,经典题)
    51Nod 1289 大鱼吃小鱼(模拟,经典好题)
    1082 与7无关的数(思维题,巨坑)
    51Nod 1003 阶乘后面0的数量(数学,思维题)
    2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
    后缀数组(一堆干货)
  • 原文地址:https://www.cnblogs.com/Afei-1123/p/11862829.html
Copyright © 2011-2022 走看看