/**
* Implement strStr().
*
* Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int strStr(string haystack, string needle)
{
if (needle.empty()) return -1;
int j = 0;
for (int i = 0; i < haystack.length(); i++)
{
for ( j = 0; j < needle.length(); j++)
{
if (haystack[i + j] != needle[j])
break;
}
if (j == needle.length())
return i;
}
return -1;
}
int main()
{
string str = "abcdefg";
string str2 = "fg";
cout << strStr(str, str2);
system("pause");
return 0;
}
【leetcode】28. Implement strStr()
最新推荐文章于 2021-02-27 02:02:17 发布