首页 > 代码库 > 28. Implement strStr()
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.
1
1 class Solution(object): 2 def strStr(self, haystack, needle): 3 """ 4 :type haystack: str 5 :type needle: str 6 :rtype: int 7 """ 8 return haystack.find(needle)
2 brute force
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ result = -1 l_needle = len(needle) l_haystack = len(haystack) if l_needle == 0 and l_haystack == 0: return 0 if l_haystack == 0 and l_needle!=0: return result if l_needle == 0 : return 0 for index in range(l_haystack): if l_haystack - index >= l_needle and needle == haystack[index:index+l_needle]: result = index break return result
28. Implement strStr()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。