[Leetcode] 28. Implement strStr()
2017-03-08 20:58
423 查看
Problem:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Idea:
Use two points to go through these two string individually.
Solution:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Idea:
Use two points to go through these two string individually.
Solution:
class Solution(object): def strStr(self, haystack, needle): i=j=0 lenhaystack = len(haystack) lenneedle = len(needle) if lenneedle == 0: return 0 while j!= lenhaystack: if haystack[j] == needle[i]: if i+1 == lenneedle: return j-i else: i += 1 j += 1 elif i != 0: j = j-i+1 i = 0 else: j += 1 return -1
相关文章推荐
- LeetCode - 28. Implement strStr()
- leetcode 28. Implement strStr()
- Leetcode 28. Implement strStr() (Easy) (cpp)
- [置顶] 【leetcode】28. Implement strStr()(Python & C++)(KMP算法)
- leetcode 28. Implement strStr() KMP 算法
- [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() java
- leetcode 28. Implement strStr()
- 【LeetCode】28. Implement strStr()
- [LeetCode] 28. Implement strStr()
- LeetCode 28. Implement strStr暴力搜索和KMP算法
- leetcode 28. Implement strStr()
- [leetcode] 28. Implement strStr() 解题报告