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)

最新文章

  1. .Net缓存管理框架CacheManager
  2. What does it mean to “delegate to a sister class” via virtual inheritance?
  3. 奔跑的歌颂 diskgenius 找回了20G数据
  4. [转]Java Web基础——Action+Service +Dao三层的功能划分
  5. Selenium FF WebDriver 遍历所有链接(另类爬虫)
  6. Yii2 RBAC 用到的表
  7. MFC中的CDC,CClientDC,CPaintDC,CWindowDC的区别
  8. python中的 zip函数详解
  9. mysql 优化点小结
  10. CSS3之背景色渐变
  11. WP8.1程序开发中,如何加载本地文件资源或安装在程序包中的资源。
  12. 17.QT-事件处理分析、事件过滤器、拖放事件
  13. web.xml 各版本的 Schema 头部声明
  14. 安装VM-tools
  15. Fiddler 教程---小坦克
  16. Xamarin Forms error MSB6006: “java.exe”已退出,代码为 2 解决办法
  17. Minimal string CodeForces – 797C
  18. PHP学习笔记 ThinkPHP
  19. POJ1862 Stripies 贪心 B
  20. WinForm版图像编辑小程序(实现图像拖动、缩放、旋转、抠图)

热门文章

  1. flat 的用法
  2. jquery 滚动事件-记录自己常用的
  3. js 条件方法、数组方法
  4. ubuntu修改apt源
  5. 使用docker构建supervisor全步骤
  6. linux下如何查询jdk的安装路径
  7. apacheTop
  8. pyquery:轻松、灵活的处理html
  9. Red Hat Enterprise Linux 6安装好,开启网卡到搭建tftp服务器和安装dnw驱动,安装samba服务器
  10. nginx理论基础