class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.size()==0 || needle.size()==0)
return 0;
int res=-1,j=0;
for(int i=0;i<haystack.size() ;i++)
{
if(haystack[i]==needle[j])
{
int pre=i;
while(haystack[i]==needle[j]&&i<haystack.size()&& j<needle.size())
{
i++;j++;
}
if(j==needle.size())
return i-j;
else
{
j=0;i=pre;
}
}
}
return -1;
}
};
11-17
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交