当前位置:网站首页>【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 indexiBefore 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 !
边栏推荐
- 渗透工具-内网权限维持-Cobalt strike
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- 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
- pwm呼吸灯
- LeetCode 6. Zigzag transformation (n-shaped transformation)
- PWM breathing lamp
- 基于多元时间序列对高考预测分析案例
- PCL point cloud image transformation
- Lampe respiratoire PWM
- Easy language ABCD sort
猜你喜欢

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

串口控制舵机转动

What if the win11 app store cannot load the page? Win11 store cannot load page

Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation

Where can I open computer administrator permissions

隐私计算技术创新及产业实践研讨会:学习

Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
![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])

寒门再出贵子:江西穷县考出了省状元,做对了什么?

Serial port controls steering gear rotation
随机推荐
pwm呼吸灯
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
P6774 [NOI2020] 时代的眼泪(分块)
Understand one article: four types of data index system
Exploration and practice of integration of streaming and wholesale in jd.com
⌈ 2022 ⌋ how to use webp gracefully in projects
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
LeetCode 5. Longest Palindromic Substring
MySQL port
618深度複盤:海爾智家的制勝方法論
你想要的宏基因组-微生物组知识全在这(2022.7)
Classic quotations
IP address translation address segment
[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
What is normal distribution? What is the 28 law?
Detailed explanation of @accessories annotation of Lombok plug-in
vscode设置删除行快捷键[通俗易懂]
LeetCode 2. 两数相加
PWM controlled steering gear
jsp 和 servlet 有什么区别?