211:添加与搜索单词 - 数据结构设计(官方)
This commit is contained in:
parent
cec7cdf42c
commit
66c2b94686
@ -0,0 +1,136 @@
|
||||
//请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
|
||||
//
|
||||
// 实现词典类 WordDictionary :
|
||||
//
|
||||
//
|
||||
// WordDictionary() 初始化词典对象
|
||||
// void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
|
||||
// bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些
|
||||
//'.' ,每个 . 都可以表示任何一个字母。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
//
|
||||
//输入:
|
||||
//["WordDictionary","addWord","addWord","addWord","search","search","search",
|
||||
//"search"]
|
||||
//[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
|
||||
//输出:
|
||||
//[null,null,null,null,false,true,true,true]
|
||||
//
|
||||
//解释:
|
||||
//WordDictionary wordDictionary = new WordDictionary();
|
||||
//wordDictionary.addWord("bad");
|
||||
//wordDictionary.addWord("dad");
|
||||
//wordDictionary.addWord("mad");
|
||||
//wordDictionary.search("pad"); // return False
|
||||
//wordDictionary.search("bad"); // return True
|
||||
//wordDictionary.search(".ad"); // return True
|
||||
//wordDictionary.search("b.."); // return True
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= word.length <= 500
|
||||
// addWord 中的 word 由小写英文字母组成
|
||||
// search 中的 word 由 '.' 或小写英文字母组成
|
||||
// 最多调用 50000 次 addWord 和 search
|
||||
//
|
||||
// Related Topics 深度优先搜索 设计 字典树 字符串 👍 328 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//211:添加与搜索单词 - 数据结构设计
|
||||
class DesignAddAndSearchWordsDataStructure {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new DesignAddAndSearchWordsDataStructure().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class WordDictionary {
|
||||
private Trie root;
|
||||
|
||||
public WordDictionary() {
|
||||
root = new Trie();
|
||||
}
|
||||
|
||||
public void addWord(String word) {
|
||||
root.insert(word);
|
||||
}
|
||||
|
||||
public boolean search(String word) {
|
||||
return dfs(word, 0, root);
|
||||
}
|
||||
|
||||
private boolean dfs(String word, int index, Trie node) {
|
||||
if (index == word.length()) {
|
||||
return node.isEnd();
|
||||
}
|
||||
char ch = word.charAt(index);
|
||||
if (Character.isLetter(ch)) {
|
||||
int childIndex = ch - 'a';
|
||||
Trie child = node.getChildren()[childIndex];
|
||||
if (child != null && dfs(word, index + 1, child)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < 26; i++) {
|
||||
Trie child = node.getChildren()[i];
|
||||
if (child != null && dfs(word, index + 1, child)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Trie {
|
||||
private Trie[] children;
|
||||
private boolean isEnd;
|
||||
|
||||
public Trie() {
|
||||
children = new Trie[26];
|
||||
isEnd = false;
|
||||
}
|
||||
|
||||
public void insert(String word) {
|
||||
Trie node = this;
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
int index = ch - 'a';
|
||||
if (node.children[index] == null) {
|
||||
node.children[index] = new Trie();
|
||||
}
|
||||
node = node.children[index];
|
||||
}
|
||||
node.isEnd = true;
|
||||
}
|
||||
|
||||
public Trie[] getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public boolean isEnd() {
|
||||
return isEnd;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Your WordDictionary object will be instantiated and called as such:
|
||||
* WordDictionary obj = new WordDictionary();
|
||||
* obj.addWord(word);
|
||||
* boolean param_2 = obj.search(word);
|
||||
*/
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,43 @@
|
||||
<p>请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。</p>
|
||||
|
||||
<p>实现词典类 <code>WordDictionary</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>WordDictionary()</code> 初始化词典对象</li>
|
||||
<li><code>void addWord(word)</code> 将 <code>word</code> 添加到数据结构中,之后可以对它进行匹配</li>
|
||||
<li><code>bool search(word)</code> 如果数据结构中存在字符串与 <code>word</code> 匹配,则返回 <code>true</code> ;否则,返回 <code>false</code> 。<code>word</code> 中可能包含一些 <code>'.'</code> ,每个 <code>.</code> 都可以表示任何一个字母。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
|
||||
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
|
||||
<strong>输出:</strong>
|
||||
[null,null,null,null,false,true,true,true]
|
||||
|
||||
<strong>解释:</strong>
|
||||
WordDictionary wordDictionary = new WordDictionary();
|
||||
wordDictionary.addWord("bad");
|
||||
wordDictionary.addWord("dad");
|
||||
wordDictionary.addWord("mad");
|
||||
wordDictionary.search("pad"); // return False
|
||||
wordDictionary.search("bad"); // return True
|
||||
wordDictionary.search(".ad"); // return True
|
||||
wordDictionary.search("b.."); // return True
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 500</code></li>
|
||||
<li><code>addWord</code> 中的 <code>word</code> 由小写英文字母组成</li>
|
||||
<li><code>search</code> 中的 <code>word</code> 由 '.' 或小写英文字母组成</li>
|
||||
<li>最多调用 <code>50000</code> 次 <code>addWord</code> 和 <code>search</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>设计</li><li>字典树</li><li>字符串</li></div></div><br><div><li>👍 328</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user