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)]
相关推荐
傻啦嘿哟1 小时前
Python正则表达式:用“模式密码“解锁复杂字符串
linux·数据库·mysql
飞桨PaddlePaddle2 小时前
Wan2.1和HunyuanVideo文生视频模型算法解析与功能体验丨前沿多模态模型开发与应用实战第六期
人工智能·算法·百度·音视频·paddlepaddle·飞桨·deepseek
浪裡遊2 小时前
Linux常用指令
linux·运维·服务器·chrome·功能测试
Starry_hello world3 小时前
C++ 快速幂算法
c++·算法·有问必答
段ヤシ.3 小时前
银河麒麟(内核CentOS8)安装rbenv、ruby2.6.5和rails5.2.6
linux·centos·银河麒麟·rbenv·ruby2.6.5·rails 5.2.6
石去皿3 小时前
力扣hot100 91-100记录
算法·leetcode·职场和发展
圣保罗的大教堂5 小时前
leetcode 2799. 统计完全子数组的数目 中等
leetcode
SsummerC5 小时前
【leetcode100】组合总和Ⅳ
数据结构·python·算法·leetcode·动态规划
深夜情感老师5 小时前
centos离线安装ssh
linux·centos·ssh
YuCaiH5 小时前
数组理论基础
笔记·leetcode·c·数组