当前位置:网站首页>【Leetcode】14. 最長公共前綴
【Leetcode】14. 最長公共前綴
2022-07-02 17:01:00 【wangzirui32】
博文作者 wangzirui32
喜歡的可以 點贊 收藏 關注哦~~
本文首發於CSDN,未經許可禁止轉載
1. 題目描述

來源:力扣(LeetCode)
鏈接:https://leetcode.cn/problems/longest-common-prefix/
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題思路
我們找到列錶中最小長度的字符串,進行循環,然後每一次循環檢測是否與其他字符串前綴相同,如相同結果存入result中,循環結束後返回最大前綴。
3. 代碼實現
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
我來解析其中比較重要的代碼:
min(strs, key=lambda s: len(s))以字符串的長度為參照,選出最小字符串swith = min_length_str[:i]獲取索引i之前的前綴all([k.startswith(swith) for k in strs])檢測其他字符串前綴是否都相同。
4. 最終結果

好了,今天的課程就到這裏,我是wangzirui32,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- Day 18 of leetcode dynamic planning introduction
- pwm呼吸灯
- 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
- Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
- 只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
- 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
- C语言自定义函数的方法
- TCP拥塞控制详解 | 2. 背景
- < IV & gt; H264 decode output YUV file
- 入行数字IC验证后会做些什么?
猜你喜欢

Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)

易语言abcd排序
![[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)](/img/3f/79dcfcd88d779a5d493b4b539bd448.jpg)
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)

Lampe respiratoire PWM

Data security industry series Salon (III) | data security industry standard system construction theme Salon
![John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])

OpenHarmony如何启动远程设备的FA

Interview summary of large factories

linux下配置Mysql授权某个用户远程访问,不受ip限制

PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
随机推荐
移动应用性能工具探索之路
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
大厂面试总结大全
618深度複盤:海爾智家的制勝方法論
Role and function of uboot
Analysis of how to prevent virus in industrial computer
vscode设置删除行快捷键[通俗易懂]
2322. 从树中删除边的最小分数(异或和&模拟)
Usage of sprintf() function in C language
基于多元时间序列对高考预测分析案例
linux下配置Mysql授权某个用户远程访问,不受ip限制
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
LeetCode 3. Longest substring without duplicate characters
What will you do after digital IC Verification?
一文看懂:数据指标体系的4大类型
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
Privacy computing technology innovation and industry practice seminar: Learning
串口控制舵机转动
pwm呼吸燈