leetCode 28. Implement strStr() 字符串
2016-08-11 01:03
330 查看
28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
在haystack中找与needle 第一个相匹配的位置。如果找不到,返回-1。
代码如下:
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
在haystack中找与needle 第一个相匹配的位置。如果找不到,返回-1。
代码如下:
class Solution { public: int strStr(string haystack, string needle) { if(haystack.size() == 0 && needle.size() == 0) return 0; if(needle.size() == 0) return 0; if(haystack.size() < needle.size()) return -1; for(int i = 0;i < haystack.size() - needle.size() + 1;i++) { bool flag = true; if(needle[0] == haystack[i]) { int j = 0; for(; j < needle.size();j++) { if(needle[j] != haystack[i+j]) { flag = false; break; } } if(flag) return i; } } return -1; } };2016-08-11 01:02:49
相关文章推荐
- leetcode_28. Implement strStr() 字符串模式匹配,返回匹配的首字符位置
- LeetCode --- 28. Implement strStr()
- leetcode | Implement strStr() | 实现字符串查找函数
- Leetcode 28. Implement strStr()
- leetcode-28. Implement strStr()
- 【一天一道LeetCode】#28. Implement strStr()
- 【LeetCode】28. Implement strStr() (2 solutions)
- LeetCode 28. Implement strStr()(实现子串定位)
- Leetcode:28. Implement strStr()(JAVA)
- [LeetCode]28. Implement strStr()
- Leetcode 28.Implement strStr() 解题报告【C库函数strstr()模拟-字符串中子串首次出现的地址】
- 【LeetCode 28_字符串_匹配】Implement strStr()
- [LeetCode] 28. Implement strStr() 解题思路
- leetcode 28. Implement strStr()
- leetcode 28. Implement strStr()
- [Leetcode] 28. Implement strStr()
- [LeetCode] Implement strStr()[字符串hash]
- <LeetCode OJ> 28. Implement strStr()
- LeetCode 28. Implement strStr()
- leetcode 28. Implement strStr()