查看: 1099| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 求帮看leetcode replace words有run time error

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
求大牛帮忙看看以下的code,明明debug了没问题可是leetcode一提交就有个case是run time error。感激不尽!```
class TrieNode {
public:
        string word;
        TrieNode* children[26];
        TrieNode() {
        }
};
class Solution {
public:
    string replaceWords(vector<string>& dict, string sentence) {
                string res;
        TrieNode* root = new TrieNode();
                for (int i = 0; i < dict.size(); i++){
                        string cur = dict[i];
                        TrieNode* curNode = root;
                        for (int j = 0; j < cur.size(); j++){
                                if (curNode->children[cur[j]-'a'] == NULL){
                                        curNode->children[cur[j]-'a'] = new TrieNode();
                                }
                                curNode = curNode->children[cur[j]-'a'];
                        }
                        curNode->word = cur;
                }
                int i = 0;
                int pre = 0;
                while (i <= sentence.size()){
                        if ((i == sentence.size()) || (sentence[i] == ' ')){
                                string cur = sentence.substr(pre,i-pre);
                                TrieNode* curNode = root;
                                for (int j = 0; j < cur.size(); j++){
                                        if (curNode->children[cur[j]-'a']!=NULL){
                                                curNode = curNode->children[cur[j]-'a'];
                                                if (curNode->word.empty() == false){
                                                        res = res + curNode->word + " ";
                                                        break;
                                                }
                                        }
                                        else{
                                                res = res + cur + " ";
                                                break;
                                        }
                                }
                                if (i >= sentence.size()){
                                        break;
                                }
                                i++;
                                pre = i;
                        }
                        else{
                                i++;
                        }
                }
                res.erase(res.begin()+res.size()-1);
                return res;
    }
};

```

上一篇:立帖刷题,把过去欠的都还上,加油!!
下一篇:Tree Travel 有什么比较系统讲解的网站或者数据吗
全局:
我把你class TrieNode里面的
TrieNode* children[26];
TrieNode() {}
改成
vector<TrieNode*> children;
TrieNode() : children(26, NULL) {}
算一个超大的case就没有runtime error了,但是得到一个wrong result。你看看其他地方逻辑是不是还有问题。
不过你现在的问题(至少一部分)是没有初始化。C的array的初值应该是undefined,这样程序效率高。
想起一句话 C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.

顺便都是public的话写成struct不就好了。
回复

使用道具 举报

全局:
确实很奇怪..

我发现的是他insert这块会出错.. 再具体的就不知道咯..
回复

使用道具 举报

🔗
hzyfree 2018-7-4 05:26:25 | 只看该作者
全局:
老哥,你要把报错的case和错误信息po出来啊
回复

使用道具 举报

全局:
弄明白了吗大胸呆
回复

使用道具 举报

🔗
 楼主| frosty 2018-7-8 12:58:52 | 只看该作者
全局:
忘记密码 发表于 2018-7-6 12:37
我把你class TrieNode里面的
TrieNode* children[26];
TrieNode() {}

多谢!之前确实debug出来一个逻辑性的问题,是sentence中某个单词比dictionary里要短的情况没有考虑进去,再加上您提议的初始化,已经过了!code如下:
```
class TrieNode {
public:
        string word;
        TrieNode* children[26];
        TrieNode() {
                for (int i = 0; i < 26; i++){
                        children[i] = NULL;
                }
        }
};
class Solution {
public:
    string replaceWords(vector<string>& dict, string sentence) {
                string res;
        TrieNode* root = new TrieNode();
                for (int i = 0; i < dict.size(); i++){
                        string cur = dict[i];
                        TrieNode* curNode = root;
                        for (int j = 0; j < cur.size(); j++){
                                if (curNode->children[cur[j]-'a'] == NULL){
                                        curNode->children[cur[j]-'a'] = new TrieNode();
                                }
                                curNode = curNode->children[cur[j]-'a'];
                        }
                        curNode->word = cur;
                }
                int i = 0;
                int pre = 0;
                while (i <= sentence.size()){
                        if ((i == sentence.size()) || (sentence[i] == ' ')){
                                string cur = sentence.substr(pre,i-pre);
                                TrieNode* curNode = root;
                                for (int j = 0; j < cur.size(); j++){
                                        if (curNode->children[cur[j]-'a']!=NULL){
                                                curNode = curNode->children[cur[j]-'a'];
                                                if (curNode->word.empty() == false){
                                                        cur = curNode->word;
                                                        break;
                                                }
                                        }
                                        else{       
                                                break;
                                        }
                                }
                                res = res + cur + " ";
                                if (i >= sentence.size()){
                                        break;
                                }
                                i++;
                                pre = i;
                        }
                        else{
                                i++;
                        }
                }
                res.erase(res.begin()+res.size()-1);
                return res;
    }
};


```
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表