当前位置:网站首页>[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.!
边栏推荐
- What if the win11 app store cannot load the page? Win11 store cannot load page
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- PWM breathing lamp
- Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
- IP address translation address segment
- 电脑自带软件使图片底色变为透明(抠图白底)
- Deep learning image data automatic annotation [easy to understand]
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
- 上传代码到远程仓库报错error: remote origin already exists.
猜你喜欢

LeetCode 2. 两数相加

Understand one article: four types of data index system

一文看懂:数据指标体系的4大类型
![[cloud native] briefly talk about the understanding of flume, a massive data collection component](/img/2d/8c4769e97fb84e98eafb7069551341.png)
[cloud native] briefly talk about the understanding of flume, a massive data collection component

Leetcode1380: lucky numbers in matrix

Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation

串口控制舵机转动

Multi task prompt learning: how to train a large language model?

易语言abcd排序

LeetCode 2. Add two numbers
随机推荐
Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
IP address translation address segment
渗透工具-内网权限维持-Cobalt strike
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
Linux Installation PostgreSQL + Patroni cluster problem
Hard core! One configuration center for 8 classes!
Executive engine module of high performance data warehouse practice based on Impala
LeetCode 6. Z 字形变换 (N字形变换)
unity Hub 登錄框變得很窄 無法登錄
js删除字符串中的子串
如何与博格华纳BorgWarner通过EDI传输业务数据?
linux安装postgresql + patroni 集群问题
二、mock平台的扩展
LeetCode 3. Longest substring without duplicate characters
Domestic relatively good OJ platform [easy to understand]
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
Privacy computing technology innovation and industry practice seminar: Learning
In MySQL and Oracle, the boundary and range of between and precautions when querying the date