当前位置:网站首页>【Leetcode】14. Longest Common Prefix
【Leetcode】14. Longest Common Prefix
2022-07-02 17:01:00 【wangzirui32】
Bowen author wangzirui32
Like can give the thumbs-up Collection Pay attention to ~~
This article was first published in CSDN, Reprint is prohibited without permission
1. Title Description
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-common-prefix/
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Their thinking
We find the minimum length string in the list , Cycle through , Then every time we loop to check whether it is the same as other string prefixes , If the same result is saved result
in , The maximum prefix is returned after the loop ends .
3. Code implementation
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
Let me parse the important code :
min(strs, key=lambda s: len(s))
Refer to the length of the string , Select the smallest stringswith = min_length_str[:i]
Get indexi
Before the prefixall([k.startswith(swith) for k in strs])
Check whether other string prefixes are the same .
4. final result
Okay , That's all for today's lesson , I am a wangzirui32, You can collect and pay attention to what you like , See you next time !
边栏推荐
- OpenHarmony如何启动远程设备的FA
- linux下配置Mysql授权某个用户远程访问,不受ip限制
- 移动应用性能工具探索之路
- < IV & gt; H264 decode output YUV file
- Student course selection system (curriculum design of Shandong Agricultural University)
- LeetCode 2. Add two numbers
- Usage of sprintf() function in C language
- LeetCode 4. Find the median (hard) of two positive arrays
- C语言中sprintf()函数的用法
- Analysis of how to prevent virus in industrial computer
猜你喜欢
Lampe respiratoire PWM
Ap和F107数据来源及处理
【云原生】简单谈谈海量数据采集组件Flume的理解
SQL solves the problem of continuous login deformation holiday filtering
Dgraph: large scale dynamic graph dataset
Leetcode1380: lucky numbers in matrix
The macrogenome microbiome knowledge you want is all here (2022.7)
Where can I open computer administrator permissions
渗透工具-内网权限维持-Cobalt strike
LeetCode 2. Add two numbers
随机推荐
C语言中sprintf()函数的用法
Learning Weekly - total issue 60 - 25th week of 2022
Where can I open computer administrator permissions
What is normal distribution? What is the 28 law?
pwm呼吸灯
隐私计算技术创新及产业实践研讨会:学习
linux安装postgresql + patroni 集群问题
R及RStudio下载安装教程(超详细)
LeetCode 5. 最长回文子串
Analysis of how to prevent virus in industrial computer
远程办公对我们的各方面影响心得 | 社区征文
Day 18 of leetcode dynamic planning introduction
Talk about an experience of job hopping and being rejected
什么是泛型?- 泛型入门篇
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
Kubernetes three open interfaces first sight
IP地址转换地址段
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
LeetCode 2. Add two numbers