当前位置:网站首页>【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 !
边栏推荐
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- LeetCode 6. Zigzag transformation (n-shaped transformation)
- unity Hub 登錄框變得很窄 無法登錄
- Deep learning image data automatic annotation [easy to understand]
- Where can I open computer administrator permissions
- C语言自定义函数的方法
- Student course selection system (curriculum design of Shandong Agricultural University)
- Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
- 数字IC手撕代码--投票表决器
- Résumé de l'entrevue de Dachang Daquan
猜你喜欢

pwm呼吸燈

Kubernetes three open interfaces first sight

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

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

易语言abcd排序

How openharmony starts FA of remote devices

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

Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品

基于Impala的高性能数仓实践之执行引擎模块

Linux Installation PostgreSQL + Patroni cluster problem
随机推荐
Machine learning perceptron model
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
[cloud native] briefly talk about the understanding of flume, a massive data collection component
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
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
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
数字IC手撕代码--投票表决器
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
入行数字IC验证后会做些什么?
PWM controlled steering gear
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
VMware install win10 image
Method of C language self defining function
二、mock平台的扩展
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share