题目
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
- 示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2 - 示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
代码
class Solution( ): 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]) else: return -1 本文链接:时光不写博客-实现strStr()
继续阅读与本文标签相同的文章
上一篇 :
php怎么接入公众号
下一篇 :
LeetCode第14题:最长公共前缀
-
Mysql 连接异常 java.math.BigInteger cannot be cast to java.lang.Long
2026-05-22栏目: 教程
-
云上爆款,上云趁现在。值得推荐的理由; 阿里云采购季省钱攻略全放送
2026-05-22栏目: 教程
-
为什么做网站SEO优化没效果?
2026-05-22栏目: 教程
-
一文读懂Apache Flink发展史
2026-05-22栏目: 教程
-
LeetCode第14题:最长公共前缀
2026-05-22栏目: 教程
