Description
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Solution
暴力法。
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.empty()&&needle.empty()) return 0;
int m=haystack.size(),n=needle.size();
for(int i=0;i<=m-n;i++){
int j=0;
for(;j<n;j++){
if(needle[j]!=haystack[i+j]) break;
}
if(j==n) return i;
}
return -1;
}
};