当前位置:网站首页>leetcode3、实现 strStr()
leetcode3、实现 strStr()
2022-07-06 01:36: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
- 【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
- Flutter Doctor:Xcode 安装不完整
- Basic operations of database and table ----- set the fields of the table to be automatically added
- Internship: unfamiliar annotations involved in the project code and their functions
- Condition and AQS principle
- Leetcode 208. Implement trie (prefix tree)
- Idea sets the default line break for global newly created files
- Loop structure of program (for loop)
- Win10 add file extension
猜你喜欢

National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
![[solved] how to generate a beautiful static document description page](/img/c1/6ad935c1906208d81facb16390448e.png)
[solved] how to generate a beautiful static document description page

Docker compose配置MySQL并实现远程连接

Loop structure of program (for loop)
![[flask] official tutorial -part3: blog blueprint, project installability](/img/fd/fc922b41316338943067469db958e2.png)
[flask] official tutorial -part3: blog blueprint, project installability

About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
![[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.
![[flask] official tutorial -part2: Blueprint - view, template, static file](/img/bd/a736d45d7154119e75428f227af202.png)
[flask] official tutorial -part2: Blueprint - view, template, static file

Maya hollowed out modeling

A picture to understand! Why did the school teach you coding but still not
随机推荐
[flask] official tutorial -part2: Blueprint - view, template, static file
General operation method of spot Silver
Leetcode skimming questions_ Verify palindrome string II
Yii console method call, Yii console scheduled task
[le plus complet du réseau] | interprétation complète de MySQL explicite
Paging of a scratch (page turning processing)
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
Initialize MySQL database when docker container starts
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
【全網最全】 |MySQL EXPLAIN 完全解讀
XSS learning XSS lab problem solution
A Cooperative Approach to Particle Swarm Optimization
Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
yii中console方法调用,yii console定时任务
干货!通过软硬件协同设计加速稀疏神经网络
Force buckle 9 palindromes
MCU lightweight system core
500 lines of code to understand the principle of mecached cache client driver
Paddle framework: paddlenlp overview [propeller natural language processing development library]
Redis-字符串类型