您的位置:首页 > 其它

[leetcode] 28. Implement strStr()

2016-07-29 11:16 453 查看
Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

解法一:

class Solution {
public:
int strStr(string haystack, string needle) {
int s1 = haystack.size();
int s2 = needle.size();
if (s1<s2) return -1;
if(s2==0) return 0;

int i = 0;
while(i<s1){
string curStr = haystack.substr(i,s1-i);
if((s1-i)<s2) {
++i;
continue;
}
int j = 0;
while(j<s2){
if(curStr[j]!=needle[j]) break;
++j;
}
if(j==s2) return i;
++i;
}
return -1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  easy leetcode