1、题目

14. Longest Common Prefix
 

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

2、我的解法

# -*- coding: utf-8 -*-
# @Time : 2020/1/29 12:28
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 14. Longest Common Prefix.py from typing import List class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
d = len(strs)
minLength = 999999
for str in strs:
thisLength = len(str)
if thisLength < minLength:
minLength = thisLength
res = ""
if d > 0:
for i in range(minLength):
n = 0
r = strs[0][i]
for j in range(d):
if r == strs[j][i]:
n = n + 1
if n == d:
break
else:
return res
res = res + r
return res
else:
return res # print(Solution().longestCommonPrefix(["flower", "flow", "flight"]))
print(Solution().longestCommonPrefix([""]))

leetCode练题——14. Longest Common Prefix的更多相关文章

  1. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

  2. 【LeetCode】LeetCode——第14题:Longest Common Prefix

    14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...

  3. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  4. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  5. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  6. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  7. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  9. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

随机推荐

  1. Iframe 高度自适应,js控制Iframe 高度自适应

     Iframe 高度自适应, js控制Iframe 高度自适应, iframe自适应高度 ================================ ©Copyright 蕃薯耀 2019年12 ...

  2. liunx详解-1

    一 文件系统 根目录结构 root root用户家目录 home 其他用户家目录 etc 系统配置目录 bin sbin 可执行二进制文件目录,sbin只有root可访问 opt 软件安装目录 usr ...

  3. .net core各种修改环境变量的方式

    除了修改系统变量,或者程序硬编码中修改,还有以下方法: 发布在IIS中,修改web.config <environmentVariables> <environmentVariabl ...

  4. PostGreSql - 提取jsonb数据

    本文主要介绍如何在PostGreSql中提取出jsonb类型字段中的某个key的值 参考:https://www.cnblogs.com/mywebnumber/p/5551092.html 一.简单 ...

  5. VS2017编写c/c++汇编函数并调用

    首先在VS里面创建个空项目,然后添加汇编文件 .asm,    右键asm文件属性  --- 常规,改成下图的设置  , 从生成中排除改为否, 项类型改为自定义生成工具 然后点确定. 再次右键asm文 ...

  6. LPR-贷款市场报价利率

    贷款市场报价利率(Loan Prime Rate)介绍: 贷款市场报价利率(Loan Prime Rate,简称LPR)是商业银行对其最优质客户执行的贷款利率,其他贷款利率可在此基础上加减点生成.贷款 ...

  7. 【Python】第一个程序---Helloworld!

    对于大多数程序语言,第一个入门编程代码便是"Hello World!",以下代码为使用Python输出"Hello World!": #!/usr/bin/py ...

  8. (爬虫)随机生成一个header

    #!/usr/bin/env python #-*- coding: utf-8 -*- #__Author__: yunrui #__Version__: 1.0 #__Time__: 2019/1 ...

  9. Hadoop3.1.1源码Client详解 : 入队前数据写入

    该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 紧接着上一篇: Hadoop3.1.1源码Client详解 : 写入准备-RPC调用与流的建立 先给出 ...

  10. AFNetworking errorCode -1016 解决方法

    AFNetworking 默认是只能解析以下格式,如果需要支持data等格式,需要增加acceptableContentTypes AFNetworking.acceptableContentType ...