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

边栏推荐
- A Cooperative Approach to Particle Swarm Optimization
- [solved] how to generate a beautiful static document description page
- selenium 元素定位(2)
- yii中console方法调用,yii console定时任务
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
- [flask] official tutorial -part3: blog blueprint, project installability
- Unreal browser plug-in
- Format code_ What does formatting code mean
- D22:indeterminate equation (indefinite equation, translation + problem solution)
猜你喜欢

Leetcode sum of two numbers

【SSRF-01】服务器端请求伪造漏洞原理及利用实例

Maya hollowed out modeling

Mongodb problem set
Folio. Ink is a free, fast and easy-to-use image sharing tool
![[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)
![[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览](/img/94/05b2ff62a8a11340cc94c69645db73.png)
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览

c#网页打开winform exe
随机推荐
[flask] obtain request information, redirect and error handling
[flask] official tutorial -part3: blog blueprint, project installability
【已解决】如何生成漂亮的静态文档说明页
500 lines of code to understand the principle of mecached cache client driver
[flask] official tutorial -part2: Blueprint - view, template, static file
Leetcode 208. Implement trie (prefix tree)
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Docker compose配置MySQL并实现远程连接
Bidding promotion process
Basic operations of database and table ----- delete data table
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
【网络攻防实训习题】
Selenium element positioning (2)
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
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
Redis-列表
Mongodb problem set
yii中console方法调用,yii console定时任务
Regular expressions: examples (1)
Superfluid_ HQ hacked analysis