这是一道leetcode上的题目,题目是这样的:
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
Subscribe to see which companies asked this question
然后呢,网上的这样的答案是可以AC的:
class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* next[26];
TrieNode() {
isWord=false;
for (int i = 0; i < 26; i++)
next[i] = NULL;
//for(auto p:next)
//{
// p=NULL;
// }
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode*node = root;
for (int i = 0; i < word.size(); i++)
{
if (node->next[word[i] – “a”] == NULL)
node->next[word[i] – “a”] = new TrieNode();
node = node->next[word[i] – “a”];
}
node->isWord = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* node=root;
for(int i=0;i<word.size();i++){
char chr=word[i];
if(node->next[chr-“a”]==NULL)
return false;
node=node->next[chr-“a”];
if(i==word.size()-1&&node->isWord==false)
return false;
}
return true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* node=root;
for(int i=0;i<prefix.size();i++){
char chr=prefix[i];
if(node->next[chr-“a”]==NULL)
return false;
node=node->next[chr-“a”];
}
return true;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert(“somestring”);
// trie.search(“key”);
然后呢,本人只改了一点就是把 TrieNode()构造函数的遍历改用for auto:
for(auto p:next)
{
p=NULL;
}
就提示runtime error。为什么呢,感觉不科学
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
Subscribe to see which companies asked this question
然后呢,网上的这样的答案是可以AC的:
class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* next[26];
TrieNode() {
isWord=false;
for (int i = 0; i < 26; i++)
next[i] = NULL;
//for(auto p:next)
//{
// p=NULL;
// }
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode*node = root;
for (int i = 0; i < word.size(); i++)
{
if (node->next[word[i] – “a”] == NULL)
node->next[word[i] – “a”] = new TrieNode();
node = node->next[word[i] – “a”];
}
node->isWord = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* node=root;
for(int i=0;i<word.size();i++){
char chr=word[i];
if(node->next[chr-“a”]==NULL)
return false;
node=node->next[chr-“a”];
if(i==word.size()-1&&node->isWord==false)
return false;
}
return true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* node=root;
for(int i=0;i<prefix.size();i++){
char chr=prefix[i];
if(node->next[chr-“a”]==NULL)
return false;
node=node->next[chr-“a”];
}
return true;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert(“somestring”);
// trie.search(“key”);
然后呢,本人只改了一点就是把 TrieNode()构造函数的遍历改用for auto:
for(auto p:next)
{
p=NULL;
}
就提示runtime error。为什么呢,感觉不科学
解决方案
10
你确定你的编译器支持C++11么?
10
leetcode应该不支持C++11
20
改成引用再试一下。
for(auto& p:next) { p=NULL; }