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