当前位置:网站首页>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刷题_平方数之和
- How to get the PHP version- How to get the PHP Version?
- Docker compose configures MySQL and realizes remote connection
- PHP error what is an error?
- leetcode刷题_验证回文字符串 Ⅱ
- 阿里测开面试题
- Loop structure of program (for loop)
- ClickOnce does not support request execution level 'requireAdministrator'
- Selenium element positioning (2)
- Basic operations of databases and tables ----- primary key constraints
猜你喜欢
Yii console method call, Yii console scheduled task
Docker compose配置MySQL并实现远程连接
NiO related knowledge (II)
干货!通过软硬件协同设计加速稀疏神经网络
Mathematical modeling learning from scratch (2): Tools
Cookie concept, basic use, principle, details and Chinese transmission
Leetcode skimming questions_ Sum of squares
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
Leetcode skimming questions_ Invert vowels in a string
Accelerating spark data access with alluxio in kubernetes
随机推荐
Electrical data | IEEE118 (including wind and solar energy)
ClickOnce 不支持请求执行级别“requireAdministrator”
Leetcode3. Implement strstr()
【Flask】响应、session与Message Flashing
Numpy array index slice
[the most complete in the whole network] |mysql explain full interpretation
Redis-字符串类型
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
How to get the PHP version- How to get the PHP Version?
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
Unity VR resource flash surface in scene
2022 PMP project management examination agile knowledge points (8)
Leetcode1961. Check whether the string is an array prefix
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
[Jiudu OJ 09] two points to find student information
Selenium waiting mode
ORA-00030
Code Review关注点
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
Maya hollowed out modeling