zoukankan      html  css  js  c++  java
  • LeetCode——Add and Search Word

    Description:

    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.

    题目很好理解:添加单词查找单词。

    首先想到的是用线性数据结构,然后逐个匹配查找。可以想象一定是超时的。

     1 public class WordDictionary {
     2     
     3     private List<String> arr = new ArrayList<String>();
     4 
     5     // Adds a word into the data structure.
     6     public void addWord(String word) {
     7         arr.add(word);
     8     }
     9 
    10     // Returns if the word is in the data structure. A word could
    11     // contain the dot character '.' to represent any one letter.
    12     public boolean search(String word) {
    13         boolean flag = true;
    14         for(String str : arr) {
    15             if(str.equals(word)) {
    16                 flag = true;
    17                 break;
    18             }
    19             else if(arr.contains(".")) {
    20                 if(word.length() != str.length()) {
    21                     flag = false;
    22                     break;
    23                 }
    24                 
    25                 for(int i=0; i<str.length();) {
    26                     char ch1 = str.charAt(i);
    27                     char ch2 = word.charAt(i);
    28                     if(ch1 == '.' || ch2 == '.' || ch1 == ch2) {
    29                         i ++;
    30                     }
    31                     else {
    32                         flag = false;
    33                         break;
    34                     }
    35                 }
    36                 
    37             }
    38             else {
    39                 flag = false;
    40                 break;
    41             }
    42         }
    43         return flag;
    44     }
    45 }
    46 
    47 // Your WordDictionary object will be instantiated and called as such:
    48 // WordDictionary wordDictionary = new WordDictionary();
    49 // wordDictionary.addWord("word");
    50 // wordDictionary.search("pattern");

    要优化明显要使用Trie树来减少无用的比较次数,从而降低时间复杂度。

    关于Trie树:http://www.cnblogs.com/wxisme/p/4876197.html

    public class WordDictionary {
        
        private TrieNode root;
        
        public WordDictionary() {
            this.root = new TrieNode();
        }
        
        
        
        private class TrieNode {
            private TrieNode[] son;
            private char val;
            private boolean isEnd;
            public TrieNode() {
                this.son = new TrieNode[26];
                this.isEnd = false;
            }
        }
        
        
        
        
    
        // Adds a word into the data structure.
        public void addWord(String word) {
            
            char[] wordChars = word.toCharArray();
            TrieNode node = this.root;
            
            for(char ch : wordChars) {
                int pos = ch - 'a';
                if(node.son[pos] == null) {
                    node.son[pos] = new TrieNode();
                    node.son[pos].val = ch;
                }
                node = node.son[pos];
            }
            node.isEnd = true;
        }
        
        
        public boolean patternSearch(String word, TrieNode node) {
            char[] wordChars = word.toCharArray();
            for(int at=0; at<word.length(); at++) {
                char ch = wordChars[at];
                if(ch != '.') {
                    int pos = ch - 'a';
                    if(node.son[pos] != null) {
                        
                        node = node.son[pos];
                    }
                    else {
                        return false;
                    }
                }
                else {
                    int flag = 0;
                    for(int i=0; i<26; i++) {
                        if(node.son[i] != null) {
                            boolean b =patternSearch(word.substring(at+1), node.son[i]);
                            if(b) return b;
                            else {
                                flag ++;
                            }
                        }
                        else {
                            flag ++;
                            continue;
                        }
                    }
                    if(flag == 26) {
                        return false;
                    }
                }
            }
            return node.isEnd;
        }
    
        // 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 patternSearch(word, this.root);
        }
    
    }
  • 相关阅读:
    nginx 详解
    阿里云 消息队列mq
    手机浏览器Yandex安装插件说明
    windows下JAVA环境变量配置
    共享文件夹免密登入
    自动添加静态路由
    加入WSUS补丁服务器并下载补丁
    加入时间同步服务器(NTP)
    更改rdp端口
    关闭及开启445等危险端口
  • 原文地址:https://www.cnblogs.com/wxisme/p/4875309.html
Copyright © 2011-2022 走看看