问题:查找字符串b在a中的起始位置,如果b不为a的子串,则返回-1

示例:

输入:a = "well", b = "el"    输出:1

输入:a="alpha", b = "am"   输出:0

Python代码:

class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0 n_len = len(needle)
h_len = len(haystack) if n_len > h_len:
return -1
elif n_len == h_len:
if haystack == needle:
return 0
return -1
for i in range(h_len-n_len+1):
if haystack[i:i+n_len] == needle:
return i
return -1

最新文章

  1. mysql之高可靠
  2. Linux--U盘安装Ubuntu12.04
  3. VMWare Workstation 10.0 Preview CN
  4. linux下expect使用教程
  5. IText 生成横向的doc文档
  6. android127 zhihuibeijing 屏幕适配
  7. Android Service with Delphi 10 Seattle
  8. SpringMVC请求分发的简单实现
  9. 345. Reverse Vowels of a String(C++)
  10. linux —— 学习笔记(环境变量的设置)
  11. java 解析 json 遍历未知key
  12. 超赞的OOM检测(除了mat以外)
  13. 面试题收集---grep和find的区别
  14. 剑指Offer——小米+小红书笔试题+知识点总结
  15. C语言实现循环队列的初始化&进队&出队&读取队头元素&判空-2
  16. MFC之几类消息的区别
  17. Java 程序员必备的 15 个框架,前 3 个地位无可动摇!
  18. Confluence 6 管理协同编辑
  19. oracle常用的数据字典查询语句
  20. linux系统转换root权限

热门文章

  1. Docker中部署puppeteer导出pdf
  2. springMvc 4.0 jackson包改变
  3. Flask简介
  4. 怎么样写一个能告诉你npm包名字是否被占用的工具
  5. Django+ajax+jsonp实现借口调用文本处理
  6. 【Java】CookieStore 类使用示例
  7. h5打电话发短信写邮件怎么实现
  8. blog集合
  9. Python实现结对编程项目
  10. hdu1085Holding Bin-Laden Captive!组合问题