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");