当前位置:网站首页>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是什麼?

边栏推荐
- Unity | two ways to realize facial drive
- Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
- 剑指 Offer 12. 矩阵中的路径
- SPIR-V初窺
- Spir - V premier aperçu
- 037 PHP login, registration, message, personal Center Design
- C web page open WinForm exe
- Idea sets the default line break for global newly created files
- [flask] obtain request information, redirect and error handling
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
猜你喜欢
![[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.

Unity | two ways to realize facial drive

Basic operations of database and table ----- set the fields of the table to be automatically added
Folio.ink 免费、快速、易用的图片分享工具

2022年PMP项目管理考试敏捷知识点(8)

Basic operations of databases and tables ----- default constraints

Mathematical modeling learning from scratch (2): Tools
![[Jiudu OJ 09] two points to find student information](/img/35/25aac51fa3e08558b1f6e2541762b6.jpg)
[Jiudu OJ 09] two points to find student information

A picture to understand! Why did the school teach you coding but still not

插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
随机推荐
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
Leetcode 208. Implement trie (prefix tree)
【已解决】如何生成漂亮的静态文档说明页
D22:indeterminate equation (indefinite equation, translation + problem solution)
干货!通过软硬件协同设计加速稀疏神经网络
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
阿里测开面试题
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
阿裏測開面試題
Docker compose configures MySQL and realizes remote connection
Flutter Doctor:Xcode 安装不完整
Crawler request module
Regular expressions: examples (1)
Basic operations of database and table ----- delete data table
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
正则表达式:示例(1)
dried food! Accelerating sparse neural network through hardware and software co design
A glimpse of spir-v
Ali test Open face test
Unreal browser plug-in