当前位置:网站首页>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??
边栏推荐
- Leetcode skimming questions_ Invert vowels in a string
- MATLB | real time opportunity constrained decision making and its application in power system
- Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
- [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.
- selenium 元素定位(2)
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- Unreal browser plug-in
- Loop structure of program (for loop)
- Redis-列表
- 【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
猜你喜欢
leetcode刷题_平方数之和
02.Go语言开发环境配置
How to upgrade kubernetes in place
selenium 等待方式
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Basic operations of databases and tables ----- default constraints
[flask] official tutorial -part3: blog blueprint, project installability
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
leetcode-两数之和
Leetcode skimming questions_ Invert vowels in a string
随机推荐
阿裏測開面試題
【Flask】响应、session与Message Flashing
【已解决】如何生成漂亮的静态文档说明页
竞赛题 2022-6-26
Shutter doctor: Xcode installation is incomplete
什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
Luo Gu P1170 Bugs Bunny and Hunter
dried food! Accelerating sparse neural network through hardware and software co design
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
leetcode刷题_反转字符串中的元音字母
Format code_ What does formatting code mean
Mongodb problem set
How does the crystal oscillator vibrate?
Reasonable and sensible
Tensorflow customize the whole training process
Folio.ink 免费、快速、易用的图片分享工具
A Cooperative Approach to Particle Swarm Optimization
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
leetcode-2.回文判断
How to upgrade kubernetes in place