473 - 单词的添加与查找

2017.9.7

注意不要越界。

对于【.】的处理,感觉自己弄的有点儿笨拙啊。后来看了别的代码,自己的思路是没错的,就是在代码的书写上,还是需要继续努力的吖。

public class WordDictionary {
        public class TrieNode{
		    char ch;
		    boolean exist;
		    TrieNode []children;
	        public TrieNode(){
		    }
		    public TrieNode(char ch){
			    this.ch = ch;
		    }
	    }
		TrieNode root;
		public WordDictionary(){
			root = new TrieNode();
		}
	    // Adds a word into the data structure.
	    public void addWord(String word) {
	        // Write your code here
	    	int length = word.length();
	    	if(length <= 0){
	    		return;
	    	}
	    	TrieNode pre = root;
	    	for(int i = 0; i < length; i++){
	    		if(pre.children == null){
		    		pre.children = new TrieNode[27];
		    	}
	    		int tmp = word.charAt(i) - 'a';
	    		if(pre.children[tmp] == null){
	    			pre.children[tmp] = new TrieNode(word.charAt(i));
	    		}
	    		if( i == length - 1){
	    			pre.children[tmp].exist = true;
	    		}
	    		pre = pre.children[tmp];
	    	}
	    }

	    // 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 root) {
	    	if(word.length() <= 0){
	    		return false;
	    	}
	    	if(root == null){
	    		return false;
	    	}
	    	TrieNode pre = root;
	    	for(int i = 0; i < word.length(); i++){
	    		if(pre == null){
	    			return false;
	    		}
	    		char ch = word.charAt(i);
	    		if(ch == '.'){
	    			for(int k = 0; k < 26; k++){
	    				if(pre.children != null && pre.children[k] != null){
	    				   if(i == word.length()-1){
	    				       if(pre.children[k].exist == true){
	    				           return true;
	    				       }
	    				       else{
	    				           continue;
	    				       }
	    				   } 
	    				   
	    				   if(search(word.substring(i+1),pre.children[k]) == true){
	    						return true;
	    					}
	    				}
	    			}
	    			return false;
	    		}
	    		int tmp = ch - 'a';
	    		if(pre.children == null || pre.children[tmp] == null){
	    			return false;
	    		}
	    		if(i == word.length() -1){
	    			if(pre.children[tmp] != null && pre.children[tmp].exist == true){
	    				return true;
	    			}
	    			else{
	    				return false;
	    			}
	    		}
	    		pre = pre.children[tmp];
	    	}
	    	
	    	return false;
	    	
	    }
	    public boolean search(String word) {
	        // Write your code here
	    	return search(word,root);
	    }
	}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

后来写了一个看起来稍显简单的代码,但是运行时间却明显增加了几乎一倍呢。看来好看的代码不一定中用啊。

public class WordDictionary {
        public class TrieNode{
		    char ch;
		    boolean exist;
		    TrieNode []children;
	        public TrieNode(){}
		    public TrieNode(char ch){
			    this.ch = ch;
		    }
	    }
		TrieNode root;
		public WordDictionary(){
			root = new TrieNode();
		}
	    // Adds a word into the data structure.
	   
	   public void addWord(String word,TrieNode root){
			if(word.length() == 0){
				return;
			}
			if(root.children == null){
				root.children = new TrieNode[26];
			}
			if(root.children[word.charAt(0)-'a'] == null){
			    root.children[word.charAt(0)-'a'] = new TrieNode(word.charAt(0));
			}
			if(word.length() == 1){
			    root.children[word.charAt(0)-'a'].exist = true;
			}
			if(word.length() > 1){
				addWord(word.substring(1),root.children[word.charAt(0)-'a']);
			}
			
		}
	    public void addWord(String word) {
	       addWord(word,root);
	    }
	    // 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 root) {
	    	if(word.length() <= 0){
	    		return false;
	    	}
	    	if(root == null){
	    		return false;
	    	}
	    	char ch = word.charAt(0);
	    	if(root.children == null){
    			return false;
    		}
	    	if(ch == '.'){
	    		for(int k = 0; k < 26; k++){
	    			if(root.children[k] != null){
	    				if(word.length() == 1 && root.children[k].exist  == true){
	    					return true;
	    				} 
	    				if(search(word.substring(1),root.children[k]) == true){
	    					return true;
	    				}
	    			}
	    		}
	    		return false;
	    	}
	    	int tmp = ch - 'a';
	    	if(root.children[tmp] == null){
	    		return false;
	    	}
	    	if(word.length() == 1 && root.children[tmp].exist == true){
	    		return true;
	    	}
	    	return search(word.substring(1),root.children[tmp]);
	    }
	    public boolean search(String word) {
	        // Write your code here
	    	return search(word,root);
	    }
	}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值