• [LeetCode] 211. Add and Search Word


    题目:

    Design a data structure that supports the following two operations:

    void addWord(word)
    bool search(word)
    

    search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

    For example:

    addWord("bad")
    addWord("dad")
    addWord("mad")
    search("pad") -> false
    search("bad") -> true
    search(".ad") -> true
    search("b..") -> true
    

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

    题意及分析:设计一个支持插入和查找的数据结构,插入的字符串只包含a-z的小写字母,查找的字符串还可包含‘.’(代表任何一个字母)。这道题可以其实就是要求一个字典树,每个树的节点有26个子节点,有一个布尔类型判断从根节点到该点组成的字符串是否是一个字母。查询时如果遇到‘.’就遍历查询当前节点的所有子节点,如果有一个符合便能查找到,否则查找不到。

    代码:

    class TrieNode{
        public char word;       //字母
        public boolean isWord;  //判断从根节点到该点是否是一个完整的单词
        public TrieNode[] childrens = new TrieNode[26];    //子节点
        public TrieNode (){}
        public TrieNode( char word0){
            TrieNode trieNode = new TrieNode();
            trieNode.word = word0;
        }
    }
    
    public class WordDictionary {
    
        TrieNode root;
        /** Initialize your data structure here. */
        public WordDictionary() {
            root = new TrieNode();
            root.word = ' ';
        }
    
        /** Adds a word into the data structure. */
        public void addWord(String word) {
            TrieNode node = root;
            for(int i=0;i<word.length();){
                while(i<word.length()&&node.childrens[word.charAt(i)-'a']!=null){
                    node =node.childrens[word.charAt(i)-'a'];
                    i++;
                }
                while (i<word.length()){
                    node.childrens[word.charAt(i)-'a'] = new TrieNode(word.charAt(i));
                    node = node.childrens[word.charAt(i)-'a'];
                    i++;
                }
            }
            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) {
            TrieNode node = root;
            return searchRes(0,word,node);
        }
    
        private boolean searchRes(int start,String word,TrieNode node) {
            if(start == word.length()){
                if(node.isWord==true) return true;
                else return false;
            }
            if(word.charAt(start)=='.'){        //如果某点为'.'
                boolean isWord = false;
                for(int i=0;i<26;i++){
                    if(node.childrens[i]!=null){
                        isWord = isWord||searchRes(start+1,word,node.childrens[i]);
                    }
                }
                return isWord;
            }else{
                if(node.childrens[word.charAt(start)-'a']!=null){
                    return searchRes(start+1,word,node.childrens[word.charAt(start)-'a']);
                }else
                    return false;
            }
        }
    }
    
    /**
     * Your WordDictionary object will be instantiated and called as such:
     * WordDictionary obj = new WordDictionary();
     * obj.addWord(word);
     * boolean param_2 = obj.search(word);
     */
  • 相关阅读:
    用Visual C#实现文件下载
    解读C#中的规则表达式
    NET(C#)连接各类数据库集锦
    C#中编写多线程(1)——起步
    C#中的常用加密算法与其它语言的兼容性
    C#的事件处理机制应用
    TCP IP协议之通信详解
    手把手教你AspNetCore WebApi:认证与授权
    TCP IP协议之初识
    mysql中exit和in的区别
  • 原文地址:https://www.cnblogs.com/271934Liao/p/7301195.html
Copyright © 2020-2023  润新知