c++ - Leetcode 28 - 实现 strStr() : question

标签 c++ string algorithm function strstr

我在 Leetcode 28 提交的内容中遇到了一个错误,迄今为止我一直没有发现这个错误。我的代码适用于大多数测试用例,但我对 haystack =“mississippi”、needle =“issip”等场景感到困惑。

我尝试过调试,发现整个 haystack 字符串都被迭代了,并且返回 -1 或未找到。每次出现“i”时找到的子字符串长度为 4, 1, 1。

int strStr(string haystack, string needle) {
        if (needle.empty()) {
            return 0;
        }
        if (haystack.empty() && !needle.empty()) {
            return -1;
        }
        int i = 0, j = 0, ans = 0;
        for (i; i < haystack.length(); i++) {
            if (haystack[i] == needle[0]) {
                j = 0;
                ans = i;
                for (j; j < needle.length(); j++) {
                    /*
                    if (haystack[i++] == needle[j]) {
                        continue;
                    }
                    else {
                        break;
                    }
                    */
                    if (haystack[i++] != needle[j]) {
                        break;
                    }
                }
                if (j == needle.length()) {
                    return ans;
                }
            }
            if (j == needle.length()) {
            return ans;
            }
        }
        return -1;
    }

输入:“密西西比”,“issip” 输出:-1(ans = 10,j = 1)

最佳答案

该函数有几个缺点。

对于初学者来说,它应该这样声明

std::string::size_type strStr( const std::string &haystack, const std::string &needle );

如果在第一个字符串中找不到第二个字符串,则函数应返回 std::string::npos就像 std::string 类的所有类似成员函数一样。

函数参数必须是常量引用类型。

该 if 语句中的条件

if (haystack.empty() && !needle.empty())

有一个冗余操作数。它可以重写为

if (haystack.empty())

这个循环

for (i; i < haystack.length(); i++) 

当第一个字符串尾部的大小小于第二个字符串的大小时应停止迭代。

在此 if 语句中

if (haystack[i++] != needle[j]) {

变量 i 递增,导致变量递增两次:一次在该语句中,第二次在循环中。

第二对这些语句

        if (j == needle.length()) {
        return ans;

是多余的。

该函数可以按照演示程序中所示的方式编写。

#include <iostream>
#include <string>

std::string::size_type strStr( const std::string &haystack, const std::string &needle )
{
    if ( needle.empty() )
    {
        return 0;
    }
    else if ( haystack.empty() )
    {
        return -std::string::npos;
    }
    else
    {
        std::string::size_type ans = std::string::npos;

        auto n1 = haystack.length();
        auto n2 = needle.length();

        for ( std::string::size_type i = 0; ans == std::string::npos && i + n2 <= n1; i++ )
        {
            std::string::size_type j = 0;
            while ( j < n2 && haystack[i+j] == needle[j] ) j++;

            if ( j == n2 ) ans = i;
        }

        return ans;
    }
}

int main() 
{
    std::string haystack( "mississippi" );
    std::string needle( "issip" );

    std::cout << strStr( haystack, needle ) << '\n';

    return 0;
}

它的输出是

4

关于c++ - Leetcode 28 - 实现 strStr() : question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378770/

相关文章:

c++ - 用C++读取文件

python - 调用所有可能的函数组合

c++ - 如何从文本 C++ 中获取 double

c++ - 如何在结构或类的 vector 中快速搜索具有特定值的对象? C++

ruby - 如何获取字符串中可用键的所有组合

php - javascript不读取字符

algorithm - 我应该买哪些数据结构和算法的书?

algorithm - UTF-8 使用的前导位计数编码技术的名称是什么?

c++ - findContours 给出内存堆错误

python - 使用 python3 从 urllib 解码字节,有更好的方法吗?