首页 > 代码库 > 28. Implement strStr()
28. Implement strStr()
https://leetcode.com/problems/implement-strstr/#/description
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Sol:
Needle is a short block string while haystack is a long block string. Move the short block to check if it is in the long block.
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ for i in range(len(haystack) - len(needle) + 1): if haystack[i:i + len(needle)] == needle: return i return -1
Other solutions may use KMP algorithm...
28. Implement strStr()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。