题目:
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从
0 开始)。如果不存在,则返回 -1 。
解题思路:
用字符串的index()方法计算
代码实现:
class Solution:
def strStr(self, haystack: str, needle: str):
if needle in haystack:
return haystack.index(needle)
else:
return -1