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)]
相关推荐
Irissgwe16 小时前
一、网络基础概念
linux·网络·websocket·网络协议·socket·linux网络编程
计算机安禾16 小时前
【c++面向对象编程】第41篇:函数模板与类模板:泛型编程的基石
开发语言·c++·算法
SilentSamsara16 小时前
属性查找顺序:实例 → 类 → 父类的完整 MRO
开发语言·python·算法·青少年编程
不知名的老吴16 小时前
浅谈:树形动态规划中的换根技巧
算法·动态规划
一条大祥脚16 小时前
2021-2022 ICPC Southwestern Europe Regional Contest
算法·深度优先·图论
红茶要加冰16 小时前
linux的例行性工作——计划任务
linux·运维·服务器
byxdaz16 小时前
Linux中查看硬件信息
linux·运维
罗湖老棍子17 小时前
The xor-longest Path(信息学奥赛一本通- P1478)
算法·字符串·字典树··lca最近公共祖先
darkdragonking17 小时前
由一次构建 OpenEuler 22.03 dnf源所了解到的
linux·运维·服务器
whuhewei17 小时前
React diff算法为什么是DFS,不是BFS
算法·react.js·深度优先