c++ - 【LeetCode】Word Pattern
怪我咯
怪我咯 2017-04-17 13:05:28
0
1
542

我在 LeetCode 上练习 Word Pattern(题目连接点这里),写的程序放在本地VS2008上跑如下实例:

pattern = "abba", str = "dog cat cat fish" should return false.

没有问题,返回的是false,但是放在LeetCode 上提交,提示错误,错误如下:

代码如下:

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        const int len = pattern.length();
        char * c = new char[len+1];
        strcpy(c, pattern.c_str() );
        char * arr[1024];
        int num = 0;
        char * pch;
        pch = strtok(c, " ");
        while (pch != NULL)
        {
            arr[num++] = pch;
            pch = strtok(NULL, " ");
        }

        vector<char> vecStr;
        vector<char *> vecPattern;

        for (int i = 0; i < num; ++i)
        {
            for (int j = 0; j < vecStr.size(); ++j)
            {
                if (vecStr[j] == str[i] && *(vecPattern[j]) != *(arr[i]) )
                {
                    return false;
                }
            }

            for (int j = 0; j < vecPattern.size(); ++j)
            {
                if (*(vecPattern[j]) == *(arr[i]) && vecStr[j] != str[i])
                {
                    return false;
                }
            }

            vecStr.push_back(str[i]);
            vecPattern.push_back(arr[i]);
        }

        return true;
    }
};

求指教。

怪我咯
怪我咯

走同样的路,发现不同的人生

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!