Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

 1 class Solution(object):
 2     def strStr(self, haystack, needle):
 3         """
 4         :type haystack: str
 5         :type needle: str
 6         :rtype: int
 7         """
 8         for i in range(len(haystack) - len(needle)+1):
 9             if haystack[i:i+len(needle)] == needle:
10                 return i
11         return -1

 

 

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if needle =='':
            return 0
        if needle in haystack:
            return len(haystack.split(needle)[0])
        return -1

 

posted on 2017-03-13 10:26  Ci_pea  阅读(158)  评论(0编辑  收藏  举报