注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
面的一家大厂。后来hr说,这一轮coding,就是dictionary,先加一些词。然后,判断,有没有。一个dot表示一个字母,任意的。fo. 和foo是match的。我做的就是用trie写的。改写了search 成strongSearch。请各位看看。
. Waral dи,
class Trie {
public TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {. .и
TrieNode cur = root;
for (char c : word.toCharArray()) {
if (cur.branches[c-'a'] == null) {
cur.branches[c-'a'] = new TrieNode();
}.1point3acres
cur = cur.branches[c-'a'];
}
cur.endWord = true;
}
. check 1point3acres for more.
public boolean strongSearch(TrieNode node, String word, int p) {
if (node == null)
return false;
char c = word.charAt(p);. Waral dи,
if (c !='.') {
if (node.branches[c-'a'] == null)
return false;. .и
else {
if(p == word.length()-1){
return node.branches[c-'a'].endWord;
}
return smartSearch(node.branches[c-'a'], word, p+1);.google и
}
} else {
for (int i = 0; i < 26; i++) {
if(node.branches[i] != null) {
if(p == word.length()-1). 1point3acres.com
return node.branches[i].endWord;
if (smartSearch(node.branches[i], word, p+1))
return true;
}.google и
}
}
return false;
}
public boolean search(String word) {
TrieNode cur = root;
for (char c : word.toCharArray()) {
if (cur.branches[c-'a'] == null) {.--
return false;
}
cur = cur.branches[c-'a'];
}
return cur.endWord;
}. 1point 3 acres
public boolean startsWith(String prefix) {
TrieNode cur = root;
for (char c : prefix.toCharArray()) {. 1point3acres
if (cur.branches[c-'a'] == null) {
return false;. From 1point 3acres bbs
}
cur = cur.branches[c-'a'];. Waral dи,
}
return true;
}
}
.1point3acres
class TrieNode {
public boolean endWord;. From 1point 3acres bbs
public TrieNode[] branches;
public TrieNode() {
endWord = false;
branches = new TrieNode[26];.1point3acres
}. Waral dи,
. From 1point 3acres bbs
|