class Solution {
public:
int strStr(string haystack, string needle) {
int l1 = haystack.size();
int l2 = needle.size();
int i,j,isfind;
for (i=0;i<=(l1-l2);i++)//边界条件i等于零时依然进行
{
if (haystack[i]==needle[0])//当有匹配出现时
{
isfind = 1;
for (j=0;j<l2;j++)
{
if (haystack[i+j]!=needle[j])//再遍历下看看是否对应相同
{
isfind = 0;
break;
}
}
if (isfind==1)//相同时
return i;
}
}
return -1;
}
};
28. 找出字符串中第一个匹配项的下标个人题解
最新推荐文章于 2023-06-30 17:47:40 发布