题目

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

答案一:

def strStr(haystack: str, needle: str) -> int:lens = len(needle)if lens==0: return 0elif needle not in haystack: return -1else:for i in range(len(haystack)):if haystack[i:i+lens]==needle:return i

答案二:

def strStr(haystack: str, needle: str) -> int:return haystack.find(needle)

LeetCode 题 - 28. 实现strStr() python实现相关推荐

  1. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  2. 学渣的刷题之旅 leetcode刷题 28. 实现 strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  3. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  4. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  5. leetcode C++ 28. 实现 strStr() 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 need

    一.C++ class Solution { public:int strStr(string haystack, string needle) {if (needle.size() == 0)ret ...

  6. LeetCode 题 - 67. 二进制求和 python实现

    题目 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" ...

  7. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  8. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  9. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

最新文章

  1. Node.js aitaotu图片批量下载Node.js爬虫1.00版
  2. Git/码云上多人协作,创建分支,上传修改
  3. 深入理解Linux内核之内存寻址
  4. hdl四位二进制计数器_四位二进制加法计数器
  5. 调节pycharm字体大_字体美化大师里的字体推荐
  6. 【layui】【laydate】设置可以选择相同的年份范围
  7. c++求n的几次方_数理统计|笔记整理(E)——Ch7-C习题课
  8. 关于java中判断字符串相等==和equal 详解
  9. 矩阵分析相关证明(一) —— 正交与投影
  10. 有向图的传递闭包实现三种实现(Warshall+DFS+BFS)
  11. SPD中赋值连接字段
  12. ajax 关闭弹窗并跳转到url_Python 爬虫 | Ajax数据爬取
  13. grpcio安装不了
  14. php dw文件上传下载,PHP-文件上传
  15. java try 性能损耗_Java上的try catch并不影响性能(转)
  16. Centos下安装FastDFS
  17. 如何调整液晶显示器保护你的视力
  18. hotmail接收邮件服务器(pop),Microsoft微软邮箱 outlook、hotmail 打开pop和imap的方法
  19. Windows 10 数据恢复与预防数据丢失指南
  20. 产品周报第26期|富文本编辑器新增预览功能;博客首页增加上次阅读频道记录……

热门文章

  1. 可输入div的问题探讨
  2. 在Windows和Azure中使用加密证书提高web.config的安全性
  3. easyui-tabs
  4. Java异常类复习总结
  5. 如何优雅的理解ECMAScript中的对象
  6. 小巧Linux发行版本,老机器也不愁
  7. 请wince驱动调试助手的作者sunrain_hjb以及wince开发者进来看看
  8. 如何使用rollup打包前端组件/库
  9. DataGuard常用命令及DG主备库开关顺序
  10. 数据可视化组件Grafana详细解读--Debian/Ubuntu上的安装