您的位置:首页 > 其它

【LeetCode】28. Implement strStr()

2018-03-16 21:15 253 查看
Description:Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:Input: haystack = "aaaaa", needle = "bba"
Output: -1代码如下:class Solution {
public:
int strStr(string haystack, string needle) {
int i=0;
int j=0;
if(haystack.size()==0 && needle.size()==0)
return 0;
for(i; i<haystack.size(); i++){
if(haystack[i] == needle[0]){
for(j=0; j<needle.size(); j++){
if(needle[j] != haystack[i+j]){
break;
}
}
}
if(j==needle.size())
return i;
}
return -1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: