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

边栏推荐
- 竞价推广流程
- Electrical data | IEEE118 (including wind and solar energy)
- Flutter Doctor:Xcode 安装不完整
- A glimpse of spir-v
- 2022年PMP项目管理考试敏捷知识点(8)
- [le plus complet du réseau] | interprétation complète de MySQL explicite
- [detailed] several ways to quickly realize object mapping
- Basic operations of database and table ----- set the fields of the table to be automatically added
- 【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
- [Jiudu OJ 09] two points to find student information
猜你喜欢

国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!

Superfluid_ HQ hacked analysis

Leetcode skimming questions_ Sum of squares

【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库

Extracting key information from TrueType font files

NiO related knowledge (II)

Electrical data | IEEE118 (including wind and solar energy)

02.Go语言开发环境配置

Docker compose配置MySQL并实现远程连接

c#网页打开winform exe
随机推荐
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
ClickOnce 不支持请求执行级别“requireAdministrator”
leetcode刷题_验证回文字符串 Ⅱ
Bidding promotion process
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
NumPy 数组索引 切片
Leetcode 208. Implement trie (prefix tree)
Leetcode skimming questions_ Verify palindrome string II
Thinking about the best practice of dynamics 365 development collaboration
A Cooperative Approach to Particle Swarm Optimization
500 lines of code to understand the principle of mecached cache client driver
Leetcode skimming questions_ Sum of squares
[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.
[detailed] several ways to quickly realize object mapping
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
Kubernetes stateless application expansion and contraction capacity
Code Review关注点
leetcode刷题_反转字符串中的元音字母
selenium 元素定位(2)
干货!通过软硬件协同设计加速稀疏神经网络