leetCode练题——28. Implement strStr()
1、题目
28. Implement strStr()——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
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
2、我的解答
用python3自带的方法 find 就可以了。。。。
1 # -*- coding: utf-8 -*- 2 # @Time : 2020/2/4 11:03 3 # @Author : SmartCat0929 4 # @Email : 1027699719@qq.com 5 # @Link : https://github.com/SmartCat0929 6 # @Site : 7 # @File : 28. Implement strStr().py 8 9 10 class Solution: 11 def strStr(self, haystack: str, needle: str) -> int: 12 a=haystack.find(needle) 13 return a 14 print(Solution().strStr("happynewyear",""))
    既能朝九晚五,又能浪迹天涯!

    
                
            
        
浙公网安备 33010602011771号