当前位置:网站首页>[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éresult
Moyenne,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'indexi
Pré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.!
边栏推荐
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- pwm呼吸燈
- Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
- 基于Impala的高性能数仓实践之执行引擎模块
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- [error record] error -32000 received from application: there are no running service protocol
- 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
- 学习周刊-总第60期-2022年第25周
- TCP拥塞控制详解 | 2. 背景
- System Verilog implements priority arbiter
猜你喜欢
linux下配置Mysql授权某个用户远程访问,不受ip限制
LeetCode 1. Sum of two numbers
Masa framework - DDD design (1)
The macrogenome microbiome knowledge you want is all here (2022.7)
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
亚马逊云科技 Community Builder 申请窗口开启
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
【云原生】简单谈谈海量数据采集组件Flume的理解
一文看懂:数据指标体系的4大类型
Serial port controls steering gear rotation
随机推荐
LeetCode 4. Find the median (hard) of two positive arrays
uboot的作用和功能
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
VMware install win10 image
寒门再出贵子:江西穷县考出了省状元,做对了什么?
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
LeetCode 5. 最长回文子串
Seven charts, learn to do valuable business analysis
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
OpenPose的使用
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
Machine learning perceptron model
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
vscode设置删除行快捷键[通俗易懂]
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
2322. 从树中删除边的最小分数(异或和&模拟)
AcWing 300. Task arrangement
Data security industry series Salon (III) | data security industry standard system construction theme Salon
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share