您的位置:首页 > 编程语言 > Python开发

28. Implement strStr() leetcode Python new season 2016

2016-01-12 11:43 417 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Show Company Tags

Show Tags

Show Similar Problems

python string is immutable .. have to do sth not optimized. may change it in the future.

class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if haystack == "" and needle == "":
return 0
if haystack == "" and needle != "":
return -1
if needle == "":
return 0
newstr = haystack.split(needle)
if len(newstr[0]) == len(haystack):
return -1
return len(newstr[0])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: