LeetCode 14. 最长公共前缀

LeetCode 14. 最长公共前缀

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

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

示例 1:

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

输出:"fl"

示例 2:

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

输出:""

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

提示:

1 <= strs.length <= 200

0 <= strsi.length <= 200

strsi 仅由小写英文字母组成

蛮力法:纵向扫描

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)]
相关推荐
曦夜日长2 小时前
Git系统篇,Git本地使用(一):从“究极进化版“到三个区域的底层原理
大数据·linux·git·elasticsearch
mmmmath_32 小时前
LeetCode.199.二叉树的右视图
数据结构·leetcode
专注仿真2 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
码爸2 小时前
支持向量机介绍与示例
算法·机器学习·支持向量机
怕浪猫2 小时前
2028编程圈很火的10个Skills
算法·面试·github
shimly1234562 小时前
给断网无 sudo ubuntu22.04 机器安装 nodejs
linux
别动我齐刘海4 小时前
Linux 系统调试学习1
linux·运维·服务器·c++·学习·机器学习·机器人
Lsetea4 小时前
OpenSSL报certificate chain too long:error 22与verify_depth深度排查
linux·https·ssl证书·openssl·证书链
不像程序员的程序媛5 小时前
linux常见文本操作命令
linux·运维·服务器
wangbing11255 小时前
开发指南149-IotDB-常用命令
linux·运维·iotdb