当前位置:网站首页>【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,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
- LeetCode 4. Find the median (hard) of two positive arrays
- Classic quotations
- Easy language ABCD sort
- 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
- Take you ten days to easily complete the go micro service series (I)
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- LeetCode 6. Z 字形变换 (N字形变换)
- What is the difference between JSP and servlet?
- Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
猜你喜欢

A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits

Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!

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

Download blender on Alibaba cloud image station

linux安装postgresql + patroni 集群问题

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

Privacy computing technology innovation and industry practice seminar: Learning

How openharmony starts FA of remote devices

Hard core! One configuration center for 8 classes!

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
随机推荐
Ranger (I) preliminary perception
寒门再出贵子:江西穷县考出了省状元,做对了什么?
System Verilog implements priority arbiter
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
SQL solves the problem of continuous login deformation holiday filtering
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
Dgraph: large scale dynamic graph dataset
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
jsp 和 servlet 有什么区别?
Privacy computing technology innovation and industry practice seminar: Learning
数字IC手撕代码--投票表决器
Interview summary of large factories
入行数字IC验证后会做些什么?
VMware install win10 image
[fluent] dart data type boolean type (boolean type definition | logical operation)
Résumé de l'entrevue de Dachang Daquan
LeetCode 2. 两数相加
【Leetcode】14. 最长公共前缀
How to choose the right kubernetes storage plug-in? (09)
Deep learning image data automatic annotation [easy to understand]