您的位置:首页 > 其它

[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:

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