当前位置:网站首页>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
- Leetcode skimming questions_ Verify palindrome string II
- Basic process and testing idea of interface automation
- Numpy array index slice
- Redis守护进程无法停止解决方案
- Format code_ What does formatting code mean
- Poj2315 football games
- Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
- leetcode刷题_反转字符串中的元音字母
- Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
猜你喜欢

Loop structure of program (for loop)
![[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览](/img/94/05b2ff62a8a11340cc94c69645db73.png)
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

Leetcode skimming questions_ Verify palindrome string II

leetcode刷题_验证回文字符串 Ⅱ

c#网页打开winform exe

Accelerating spark data access with alluxio in kubernetes

Superfluid_ HQ hacked analysis

Redis-字符串类型

MySQL learning notes 2
随机推荐
SPIR-V初窺
c#网页打开winform exe
Unity | two ways to realize facial drive
Selenium element positioning (2)
Tensorflow customize the whole training process
Extracting key information from TrueType font files
Unity VR resource flash surface in scene
ClickOnce does not support request execution level 'requireAdministrator'
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
竞价推广流程
[flask] official tutorial -part2: Blueprint - view, template, static file
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
01.Go语言介绍
[flask] obtain request information, redirect and error handling
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
Redis-字符串类型
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Kubernetes stateless application expansion and contraction capacity