LeetCode 14. 最长公共前缀

LeetCode 14. 最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

示例 1:

输入:strs = ["flower","flow","flight"]

输出:"fl"

示例 2:

输入:strs = ["dog","racecar","car"]

输出:""

解释:输入不存在公共前缀。

提示:

1 <= strs.length <= 200

0 <= strs[i].length <= 200

strs[i] 仅由小写英文字母组成

蛮力法:纵向扫描

python 复制代码
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        res = ""

        for index in range(200):
            for s in strs:
                if len(s) > index:
                    if len(res) <= index:
                        res += s[index]
                    if res[index] != s[index]:
                        return res[:-1]
                else:
                    return res[:len(s)]
相关推荐
Lris-KK2 分钟前
【Leetcode】高频SQL基础题--1164.指定日期的产品价格
sql·leetcode
Moonbit29 分钟前
月报Vol.03: 新增Bitstring pattern支持,构造器模式匹配增强
后端·算法·github
快手技术33 分钟前
多模态大模型Keye-VL-1.5发布!视频理解能力更强!
算法
工藤新一¹40 分钟前
Linux —— 虚拟进程地址空间
linux·运维·服务器·c/c++·虚拟进程地址空间
Aspiresky1 小时前
浅析Linux内核scatter-gather list实现
linux·dma·scatter/gather
薛定谔的算法1 小时前
JavaScript数组操作完全指南:从基础到高级
前端·javascript·算法
可爱的小小小狼1 小时前
算法:位运算
算法
VisionPowerful1 小时前
九.弗洛伊德(Floyd)算法
算法·c#
可爱的小小小狼1 小时前
算法:哈希表
redis·算法·散列表
奔跑吧 android1 小时前
【linux kernel 常用数据结构和设计模式】【数据结构 3】【模拟input子系统input_dev和input_handler之间的多对多关系】
linux·数据结构·input·kernel·input_dev·input_handler·input_handle