week22
211 - 添加与搜索单词 - 数据结构设计
class WordDictionary {
public:
struct Node
{
Node* son[26];
bool is_end;
Node()
{
is_end = false;
for(int i = 0; i < 26; i++)
son[i] = nullptr;
}
}*root;
/** Initialize your data structure here. */
WordDictionary() {
root = new Node();
}
/** Adds a word into the data structure. */
void addWord(string word) {
auto p = root;
for(auto c : word)
{
int u = c - 'a';
if(!p->son[u]) p->son[u] = new Node();
p = p->son[u];
}
p->is_end = true;
}
bool find_word(Node* p, string& word, int si)
{
if(si == word.size()) return p->is_end;
if(word[si] == '.')
{
for(int i = 0; i < 26; i++)
if(p->son[i] && find_word(p->son[i], word, si + 1))
return true;
}
else
{
int u = word[si] - 'a';
if(p->son[u]) return find_word(p->son[u], word, si + 1);
else return false;
}
return false;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return find_word(root, word, 0);
}
};212 - 单词搜索II

213 - 打家劫舍II
214 - 最短回文串

215 - 数组中的第K个最大元素
216 - 组合总和
217 - 存在重复元素
218 - 天际线问题

219 - 存在重复元素II
220 - 存在重复元素III
Last updated