leetcode 28 实现strStr()
题目
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = "hello", needle = "ll" 输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba" 输出: -1
说明:
当 needle
是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle
是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
解题代码
class Solution:def strStr(self, haystack, needle):""":type haystack: str:type needle: str:rtype: int"""if not needle: #如果不存在返回0return 0for i in range(len(haystack)-len(needle)+1): #计算一共有个多少种遍历可能if haystack[i:i+len(needle)]==needle:return ireturn -1
leetcode 28 实现strStr()相关推荐
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- LeetCode - 28. Implement strStr()
28. Implement strStr() Problem's Link -------------------------------------------------------------- ...
- leetCode 28. Implement strStr() 字符串
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- strstr函数_[LeetCode] 28. 实现strStr()
题目链接: https://leetcode-cn.com/problems/implement-strstr/ 题目描述 实现 strStr() 函数. 给定一个 haystack 字符串和一个 n ...
- LeetCode 28. 实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- leetcode 28.实现strStr()
题目 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在 ...
- leetcode 28. Implement strStr() 实现strStr()
C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...
- LeetCode 28 实现 strStr()
https://leetcode-cn.com/problems/implement-strstr/ 解决方案 class Solution {public int strStr(String hay ...
- Leetcode 28. 实现 strStr()
原题链接 解:KMP算法 class Solution { public:int strStr(string s, string p) {if (p.empty()) return 0;int n = ...
最新文章
- request threaded-only IRQs with IRQF_ONESHOT【转】
- linux下安装配置使用memcache,memcached,libevent(后有ubuntu的memcache安装)
- 014_Collections常用方法
- python字符串逆序输出代码_一行代码实现字符串逆序输出
- python删除文件夹中的jpg_Python简单删除目录下文件以及文件夹的方法
- Android注解支持(Support Annotations)
- 前端开发~uni-app ·[项目-仿糗事百科] 学习笔记 ·004【App.vue引入全局公共样式】
- php 修改json数组的值,php – 无法通过str_replace更改JSON数组中的值
- GridBagLayout用法
- win10 wlan 诊断显示后没有有效的ip配置
- .ppt和.pptx有什么区别
- 树莓派(Raspberry Pi) 命令行下如何配置wifi(wlan)
- Verifying dml pool data
- TiDB-explain详解
- 叮咚~您的MySQL云备份已上线
- SQL中附加的基本运算
- 无人机基于目标检测的路径规划任务
- win7 ads出现Unhandled exception:c0000005
- 狼在猪面前自杀了(超级搞笑)
- bzoj3585 mex
热门文章
- Python之父加入微软,一开口就知道是老“凡学家”了
- 几种常见的微服务架构方案——ZeroC IceGrid、Spring Cloud、基于消息队列、Docker Swarm...
- 会说话的代码——书写自表达代码之道
- python里面两个大于号_听说92.8%的人答不对这道Python题,我不信,后来我信了!真有趣...
- 蓝桥杯2015年第六届javaB组省赛第一题-三角形面积
- Kotlin — 使用IDEA运行第一个Kotlin程序,打印“Hello World”!
- Flutter 《从0到1构建大前端应用》-所有知识点架构
- 阿里云服务器如何修改密码
- MTK平台调试mipi屏问题小结
- B. Forgery