28. 找出字符串中第一个匹配项的下标
决定转前端了。先用c++刷题刷着先
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.size() < needle.size()) return -1;
for(int i = 0; i <= haystack.size() - needle.size(); ++i){
int j = 0;
while(j < needle.size()){
if(haystack[i + j] == needle[j]){
++j;
}else{
break;
}
}
if(j == needle.size()) return i;
}
return -1;
}
};