LeetCode 题 - 28. 实现strStr() python实现
题目
实现 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实现相关推荐
- leetcode python3 简单题28. Implement strStr()
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...
- 学渣的刷题之旅 leetcode刷题 28. 实现 strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- LeetCode - Easy - 28. Implement strStr()
Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...
- 【LeetCode】28. Implement strStr()
Difficulty:easy More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...
- leetcode C++ 28. 实现 strStr() 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 need
一.C++ class Solution { public:int strStr(string haystack, string needle) {if (needle.size() == 0)ret ...
- LeetCode 题 - 67. 二进制求和 python实现
题目 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" ...
- LeetCode - 28. Implement strStr()
28. Implement strStr() Problem's Link -------------------------------------------------------------- ...
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- LeetCode 刷题之路(python版)
摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...
最新文章
- Node.js aitaotu图片批量下载Node.js爬虫1.00版
- Git/码云上多人协作,创建分支,上传修改
- 深入理解Linux内核之内存寻址
- hdl四位二进制计数器_四位二进制加法计数器
- 调节pycharm字体大_字体美化大师里的字体推荐
- 【layui】【laydate】设置可以选择相同的年份范围
- c++求n的几次方_数理统计|笔记整理(E)——Ch7-C习题课
- 关于java中判断字符串相等==和equal 详解
- 矩阵分析相关证明(一) —— 正交与投影
- 有向图的传递闭包实现三种实现(Warshall+DFS+BFS)
- SPD中赋值连接字段
- ajax 关闭弹窗并跳转到url_Python 爬虫 | Ajax数据爬取
- grpcio安装不了
- php dw文件上传下载,PHP-文件上传
- java try 性能损耗_Java上的try catch并不影响性能(转)
- Centos下安装FastDFS
- 如何调整液晶显示器保护你的视力
- hotmail接收邮件服务器(pop),Microsoft微软邮箱 outlook、hotmail 打开pop和imap的方法
- Windows 10 数据恢复与预防数据丢失指南
- 产品周报第26期|富文本编辑器新增预览功能;博客首页增加上次阅读频道记录……