[leetcode]: 28. Implement strStr()
2017-06-18 17:23
357 查看
1.题目
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
手动实现strStr()函数,返回子字符串needle在字符串haystack中第一次出现的位置
2.分析
暂时还没去学习KMP,所以就手动遍历匹配字符串了。3.代码
class Solution { public: int strStr(string haystack, string needle) { if (needle.size()==0) return 0; if (haystack.size()<needle.size()) return -1; for (int i = 0; i <= haystack.size() - needle.size(); i++) { int j = 0; while (j<needle.size() && haystack[i + j] == needle[j]) ++j; if (j == needle.size()) return i; } return -1; } };
相关文章推荐
- leetcode 28. Implement strStr() 实现strStr()函数
- Sunday算法的一种Java实现(Leetcode28. Implement strStr())
- Leetcode 28. Implement strStr() KMP算法解题
- [LeetCode] 28. Implement strStr() 实现strStr()函数
- LeetCode 28. Implement strStr()
- LeetCode笔记:28. Implement strStr()
- python写算法题:leetcode: 28. 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()
- [leetcode]28. Implement strStr()
- LeetCode 28. Implement strStr()
- Leetcode---28. Implement strStr()
- 2017.10.21 LeetCode - 13. Roman to Integer - 28. Implement strStr()
- LeetCode-28. Implement strStr()
- leetcode 28. Implement strStr()
- LeetCode 28. Implement strStr()