#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle)
{
int j = 0;
for (int i = 0; i < haystack.size(); i++)
{
if (haystack[i] == needle[j])
{
if (j == (needle.size() - 1))
{
return (i - j);
}
j++;
}
else
{
j = 0;
}
}
return -1;
}
};
int main()
{
Solution a;
string haystack = "sadbutsad";
string needle = "db";
cout << a.strStr(haystack, needle) << endl;
return 0;
}
16.力扣c++刷题-->找出字符串中第一个匹配的子串
最新推荐文章于 2024-07-09 23:43:07 发布