力扣第28题:实现strStr()
实例:
输入:haystack = "hello", needle = "ll" 输出:2
输入:haystack = "aaaaa", needle = "bba" 输出:-1
暴力破解法实现实现:
class Solution {
public:
int strStr(string haystack, string needle)
{
int i=0;
int j=0;
while(i<=haystack.length()&&j<=needle.length())
{
if(haystack[i]==needle[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
if(j>=needle.length())
{
return i-needle.length();
}
}
return -1;
}
};