当前位置:网站首页>Leetcode3, implémenter strstr ()
Leetcode3, implémenter strstr ()
2022-07-06 01:41:00 【L'Invincible est moi.】
Réalisation strStr() Fonctions.
Voici deux chaînes. haystack Et needle ,S'il vous plaît haystack Trouver dans la chaîne needle La première position où la chaîne apparaît(Indice de 0 C'est parti.).S'il n'existe pas,Renvoie -1 .
Description:
Quand needle Quand il s'agit d'une chaîne vide,Quelle valeur devrions - nous retourner?C'est une bonne question pour l'interview.
Pour cette question,Quand needle Quand il s'agit d'une chaîne vide, nous devrions retourner 0 .Ceci est lié à C Langue strstr() Et Java De indexOf() La définition correspond.
Exemple 1:
Entrée:haystack = "hello", needle = "ll"
Produits:2
Exemple 2:
Entrée:haystack = "aaaaa", needle = "bba"
Produits:-1
Conseils:
1 <= haystack.length, needle.length <= 104
haystack Et needle Composé uniquement de caractères anglais minuscules
Réponses personnelles
Méthode 1:
def strStr(str1,str2):
return str1.find(str2)
str1="hello"
str2="ll"
print(strStr(str1,str2))
Cette solution utilise des fonctions intégrées à la chaîne , Trop opportuniste .
Méthode 2:
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
Penser par soi - même , Par tranchage .( C'est ce qui s'est passé dans la dernière chaîne de palindromes ,On peut voir que, Même si la question est terminée ,Il faut aussireview, C'est bon pour faire des questions plus tard .)
Réponse officielle
C'est vrai, J'ai utilisé une correspondance violente ,Mais...KMPQu'est - ce que c'est??

边栏推荐
猜你喜欢

3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video

Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

Leetcode skimming questions_ Invert vowels in a string

NumPy 数组索引 切片

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

一圖看懂!為什麼學校教了你Coding但還是不會的原因...

Extracting key information from TrueType font files

Yii console method call, Yii console scheduled task

Unity | two ways to realize facial drive
随机推荐
Unity | two ways to realize facial drive
剑指 Offer 38. 字符串的排列
LeetCode 322. Change exchange (dynamic planning)
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
干货!通过软硬件协同设计加速稀疏神经网络
Win10 add file extension
MATLB | real time opportunity constrained decision making and its application in power system
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
ctf. Show PHP feature (89~110)
剑指 Offer 12. 矩阵中的路径
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
Mathematical modeling learning from scratch (2): Tools
Extracting key information from TrueType font files
NiO related knowledge (II)
leetcode-两数之和
Unreal browser plug-in
internship:项目代码所涉及陌生注解及其作用