【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;
}
};
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;
}
};
相关文章推荐
- leetcode 28. Implement strStr()
- LeetCode 28. Implement strStr()
- LeetCode - 28. Implement strStr()
- Leetcode 28. Implement strStr() (Easy) (cpp)
- leetcode 28. Implement strStr()
- leetcode28. Implement strStr()
- leetcode 28. Implement strStr()
- [Leetcode] 28. Implement strStr() 解题报告
- Sunday算法的一种Java实现(Leetcode28. Implement strStr())
- leetcode-28. Implement strStr()
- LeetCode --- 28. Implement strStr()
- leetcode 28. Implement strStr()
- LeetCode 28. Implement strStr()
- leetcode 28. Implement strStr()
- leetcode 28. Implement strStr() 实现strStr()函数
- 【LeetCode】28. Implement strStr() 解题报告
- LeetCode 28. Implement strStr暴力搜索和KMP算法
- Leetcode刷题记——28. Implement strStr(实现strStr)
- leetcode-28. Implement strStr()
- LeetCode----28. Implement strStr()