当前位置:网站首页>【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 !
边栏推荐
- Multi task prompt learning: how to train a large language model?
- In MySQL and Oracle, the boundary and range of between and precautions when querying the date
- DGraph: 大规模动态图数据集
- Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
- 小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
- 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
- Role and function of uboot
- C语言中sprintf()函数的用法
- Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
- uboot的作用和功能
猜你喜欢

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

TCP congestion control details | 2 background

小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求

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

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

大廠面試總結大全

Interview summary of large factories

上传代码到远程仓库报错error: remote origin already exists.

电脑自带软件使图片底色变为透明(抠图白底)

亚马逊云科技 Community Builder 申请窗口开启
随机推荐
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
DGraph: 大规模动态图数据集
Yolov5 practice: teach object detection by hand
linux安装postgresql + patroni 集群问题
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
Download blender on Alibaba cloud image station
LeetCode 6. Zigzag transformation (n-shaped transformation)
Domestic relatively good OJ platform [easy to understand]
一文看懂:数据指标体系的4大类型
The macrogenome microbiome knowledge you want is all here (2022.7)
入行数字IC验证后会做些什么?
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
C语言自定义函数的方法
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
OpenHarmony如何启动远程设备的FA
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
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 3. 无重复字符的最长子串
流批一体在京东的探索与实践