当前位置:网站首页>【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 !
边栏推荐
- Ranger (I) preliminary perception
- Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
- Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
- PCL least median square method fitting plane
- Hard core! One configuration center for 8 classes!
- 渗透工具-内网权限维持-Cobalt strike
- Seven charts, learn to do valuable business analysis
- Kubernetes three open interfaces first sight
- 易语言abcd排序
- 大厂面试总结大全
猜你喜欢

The macrogenome microbiome knowledge you want is all here (2022.7)

Machine learning perceptron model

Data security industry series Salon (III) | data security industry standard system construction theme Salon

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

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

How to choose the right kubernetes storage plug-in? (09)

OpenHarmony如何启动远程设备的FA

Lampe respiratoire PWM

关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...

如何与博格华纳BorgWarner通过EDI传输业务数据?
随机推荐
LeetCode 1. Sum of two numbers
渗透工具-内网权限维持-Cobalt strike
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
基于多元时间序列对高考预测分析案例
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
亚马逊云科技 Community Builder 申请窗口开启
LeetCode 1. 两数之和
jsp 和 servlet 有什么区别?
VMware install win10 image
Analysis of how to prevent virus in industrial computer
Dgraph: large scale dynamic graph dataset
PCL point cloud image transformation
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
TCP拥塞控制详解 | 2. 背景
Hard core! One configuration center for 8 classes!
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
MOSFET器件手册关键参数解读
【Leetcode】13. 罗马数字转整数