leetcode-easy-string-28 Implement strStr()
2024-09-05 16:25:05
mycode 77.15%
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
len_1 , len_2 = len(haystack) , len(needle)
for i in range(len_1 - len_2 + 1):
if haystack[i:i+len_2] == needle:
return i
return -1
参考:
要习惯python的.find操作呀
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().
class Solution(object):
def strStr(self, haystack, needle):
if not needle:
return 0
return haystack.find(needle)
最新文章
- .Net缓存管理框架CacheManager
- What does it mean to “delegate to a sister class” via virtual inheritance?
- 奔跑的歌颂 diskgenius 找回了20G数据
- [转]Java Web基础——Action+Service +Dao三层的功能划分
- Selenium FF WebDriver 遍历所有链接(另类爬虫)
- Yii2 RBAC 用到的表
- MFC中的CDC,CClientDC,CPaintDC,CWindowDC的区别
- python中的 zip函数详解
- mysql 优化点小结
- CSS3之背景色渐变
- WP8.1程序开发中,如何加载本地文件资源或安装在程序包中的资源。
- 17.QT-事件处理分析、事件过滤器、拖放事件
- web.xml 各版本的 Schema 头部声明
- 安装VM-tools
- Fiddler 教程---小坦克
- Xamarin Forms error MSB6006: “java.exe”已退出,代码为 2 解决办法
- Minimal string CodeForces – 797C
- PHP学习笔记 ThinkPHP
- POJ1862 Stripies 贪心 B
- WinForm版图像编辑小程序(实现图像拖动、缩放、旋转、抠图)