easy题,考虑一下异常就可以了
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if not needle: return 0 need_length = len(needle) length = len(haystack) for i in range(length): if length-i < need_length: return -1 if haystack[i:i+need_length] == needle: return i return -1