这道题挺简单的,双指针,直接放代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int n=-1;
for(int i=0;i<haystack.size();i++){
if(needle[n+1]==haystack[i]){
for(int j=0;j<needle.size();j++){
if(needle[n+j+1]!=haystack[i+j]) break;
if(j==needle.size()-1) return i;
}
}
}
return n;
}
};