当前位置:网站首页>leetcode3、實現 strStr()
leetcode3、實現 strStr()
2022-07-06 01:41:00 【東方不敗就是我】
實現 strStr() 函數。
給你兩個字符串 haystack 和 needle ,請你在 haystack 字符串中找出 needle 字符串出現的第一個比特置(下標從 0 開始)。如果不存在,則返回 -1 。
說明:
當 needle 是空字符串時,我們應當返回什麼值呢?這是一個在面試中很好的問題。
對於本題而言,當 needle 是空字符串時我們應當返回 0 。這與 C 語言的 strstr() 以及 Java 的 indexOf() 定義相符。
示例 1:
輸入:haystack = "hello", needle = "ll"
輸出:2
示例 2:
輸入:haystack = "aaaaa", needle = "bba"
輸出:-1
提示:
1 <= haystack.length, needle.length <= 104
haystack 和 needle 僅由小寫英文字符組成
個人解答
方法一:
def strStr(str1,str2):
return str1.find(str2)
str1="hello"
str2="ll"
print(strStr(str1,str2))
這個解答用了字符串內置的函數,太投機取巧。
方法二:
class Solution:
def strStr(self,str1,str2):
i=0
while(i<len(str1)):
if((str1[i:i+len(str2)])==str2):
return i
else:
i=i+1
else:
return -1
自己思路,采用切片方法。(是之前回文串時候的思路,由此可見,就算題做完,也一定要review,對以後做題有好處。)
官方解答
沒錯,我用的是暴力匹配,但KMP是什麼?

边栏推荐
- Docker compose configures MySQL and realizes remote connection
- c#网页打开winform exe
- Redis-Key的操作
- selenium 等待方式
- leetcode-2. Palindrome judgment
- Basic operations of databases and tables ----- default constraints
- How to upgrade kubernetes in place
- [ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
- How does the crystal oscillator vibrate?
- Mathematical modeling learning from scratch (2): Tools
猜你喜欢

NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】

A Cooperative Approach to Particle Swarm Optimization

【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库

Yii console method call, Yii console scheduled task

3D model format summary
![[Jiudu OJ 09] two points to find student information](/img/35/25aac51fa3e08558b1f6e2541762b6.jpg)
[Jiudu OJ 09] two points to find student information

UE4 unreal engine, editor basic application, usage skills (IV)
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

leetcode3、实现 strStr()
![[flask] official tutorial -part3: blog blueprint, project installability](/img/fd/fc922b41316338943067469db958e2.png)
[flask] official tutorial -part3: blog blueprint, project installability
随机推荐
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
Tensorflow customize the whole training process
【全網最全】 |MySQL EXPLAIN 完全解讀
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
Thinking about the best practice of dynamics 365 development collaboration
Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization
【详细】快速实现对象映射的几种方式
Open source | Ctrip ticket BDD UI testing framework flybirds
Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
leetcode刷题_验证回文字符串 Ⅱ
Docker compose配置MySQL并实现远程连接
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
internship:项目代码所涉及陌生注解及其作用
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
Mathematical modeling learning from scratch (2): Tools
【Flask】静态文件与模板渲染
阿裏測開面試題
Leetcode skimming questions_ Sum of squares
阿里测开面试题