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] 仅由小写英文字母组成

蛮力法:纵向扫描

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. LeetCode 14 公共前缀

    2024-06-19 03:46:01       47 阅读
  2. leetCode算法—14. 公共前缀

    2024-06-19 03:46:01       46 阅读
  3. LeetCode 14. 公共前缀

    2024-06-19 03:46:01       7 阅读
  4. 14.公共前缀

    2024-06-19 03:46:01       4 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 03:46:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 03:46:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 03:46:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 03:46:01       18 阅读

热门阅读

  1. 部署YUM仓库及NFS共享服务

    2024-06-19 03:46:01       6 阅读
  2. LeetCode-day08-881. 救生艇

    2024-06-19 03:46:01       8 阅读
  3. linux hosts配置 ip映射

    2024-06-19 03:46:01       7 阅读
  4. Web前端中的jQuery:深度解析与应用探索

    2024-06-19 03:46:01       8 阅读
  5. typescript-泛型

    2024-06-19 03:46:01       5 阅读