当前位置:网站首页>【Leetcode】14. 最长公共前缀
【Leetcode】14. 最长公共前缀
2022-07-02 14:18: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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- sql解决连续登录问题变形-节假日过滤
- jsp 和 servlet 有什么区别?
- Hard core! One configuration center for 8 classes!
- PWM controlled steering gear
- Deep learning image data automatic annotation [easy to understand]
- Penetration tool - intranet permission maintenance -cobalt strike
- uboot的作用和功能
- Interview summary of large factories
猜你喜欢
![[cloud native] briefly talk about the understanding of flume, a massive data collection component](/img/2d/8c4769e97fb84e98eafb7069551341.png)
[cloud native] briefly talk about the understanding of flume, a massive data collection component

PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御

Multi task prompt learning: how to train a large language model?
![[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](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[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

Routing mode: hash and history mode

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

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

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

OpenPose的使用

SQL solves the problem of continuous login deformation holiday filtering
随机推荐
uboot的作用和功能
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
[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
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
串口控制舵机转动
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
PCL least median square method fitting plane
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
寒门再出贵子:江西穷县考出了省状元,做对了什么?
Serial port controls steering gear rotation
Machine learning perceptron model
Take you ten days to easily complete the go micro service series (I)
Vscode setting delete line shortcut [easy to understand]
Hard core! One configuration center for 8 classes!
Where can I open computer administrator permissions
What is the difference between JSP and servlet?
LeetCode 1. Sum of two numbers
LeetCode 1. 两数之和
System Verilog实现优先级仲裁器