• <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;
        }
    }
  • 相关阅读:
    Core Foundation框架(2)命名规范,内省
    Core Foundation框架(1)基础介绍
    Swift 数组,字典
    Swift 操作符
    Swift 可选值
    Swift 元组
    Swift 变量声明
    iOS开发_UI_AutoLayout
    iOS开发_Objective-C_字符串操作
    iOS开发_Objective-C_监听搜索时用户输入的拼音
  • 原文地址:https://www.cnblogs.com/Afei-1123/p/11862829.html
Copyright © 2020-2023  润新知