当前位置:网站首页>[leetcode] 14. Préfixe public le plus long
[leetcode] 14. Préfixe public le plus long
2022-07-02 17:01:00 【Wangzirui32】
Blogueur wangzirui32
Oui. - Oui. Collection Attention!~~
Cet article a été publié pour la première fois àCSDN,Reproduction interdite sans autorisation
Table des matières
1. Description du sujet

Source::Boucle de force(LeetCode)
Liens:https://leetcode.cn/problems/longest-common-prefix/
Le droit d'auteur est la propriété du réseau de saisie.Pour les réimpressions commerciales, veuillez contacter l'autorisation officielle,Reproduction non commerciale Veuillez indiquer la source.
2. Comment résoudre le problème
Nous avons trouvé la chaîne la plus courte de la liste,Faire un cycle,Chaque boucle vérifie ensuite si elle est identique aux autres préfixes de chaîne,Si le même résultat est stockéresultMoyenne,Renvoie le préfixe maximum après la fin du cycle.
3. Mise en œuvre du Code
Code:
class Solution:
def longestCommonPrefix(self, strs) -> str:
min_length_str = min(strs, key=lambda s: len(s))
result = ""
for i in range(1, len(min_length_str)+1):
swith = min_length_str[:i]
if all([k.startswith(swith) for k in strs]):
result = swith
return result
Je vais analyser le Code le plus important :
min(strs, key=lambda s: len(s))Avec la longueur de la chaîne comme référence , Sélectionner la chaîne minimaleswith = min_length_str[:i]Obtenir l'indexiPréfixe précédentall([k.startswith(swith) for k in strs])Détecter si tous les autres préfixes de chaîne sont identiques .
4. Résultat final

C'est bon,Voici le cours d'aujourd'hui,Je suiswangzirui32,Comme vous pouvez commander une collection et l'attention,À la prochaine fois.!
边栏推荐
- Role and function of uboot
- 电脑自带软件使图片底色变为透明(抠图白底)
- AcWing 300. Task arrangement
- 七张图,学会做有价值的经营分析
- Seven charts, learn to do valuable business analysis
- Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
- Day 18 of leetcode dynamic planning introduction
- 移动应用性能工具探索之路
- VMware install win10 image
- [fluent] dart data type boolean type (boolean type definition | logical operation)
猜你喜欢

Dgraph: large scale dynamic graph dataset

Take you ten days to easily complete the go micro service series (I)

Interview summary of large factories

unity Hub 登錄框變得很窄 無法登錄

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

上传代码到远程仓库报错error: remote origin already exists.

OpenHarmony如何启动远程设备的FA

【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
![john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])

LeetCode 2. 两数相加
随机推荐
LeetCode 5. Longest Palindromic Substring
串口控制舵机转动
远程办公对我们的各方面影响心得 | 社区征文
易语言abcd排序
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Hard core! One configuration center for 8 classes!
js删除字符串中的子串
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
System Verilog实现优先级仲裁器
LeetCode 1. Sum of two numbers
PWM breathing lamp
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
如何与博格华纳BorgWarner通过EDI传输业务数据?
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Talk about an experience of job hopping and being rejected
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
OpenHarmony如何启动远程设备的FA
AcWing 300. Task arrangement