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. 工具 - deepin vscode中的oh-my-zsh乱码

    解决办法 https://blog.zhaytam.com/2019/04/19/powerline-and-zshs-agnoster-theme-in-vs-code/ git clone htt ...

  2. Abp中打开错误信息输出

    Abp默认是不向客户端输出错误日志的,导致有些内部报错既不能中断,也没有信息输出.此时打开日志输出开关即可.在xxModule类中PreInitialize()方法里 Configuration.Mo ...

  3. php操作shee学习笔记之(一)PHP操作shell函数

    一.php操作shell 1.system函数:执行普通命令 string system (string $command [,int &$return_var]) 1)$command是命令 ...

  4. 路飞-pip源

    pip安装源 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里: ...

  5. CDQ 分治

    引言: 什么是CDQ分治?其实这是一种思想而不是具体算法,因此CDQ分治覆盖的范围相当广泛,在 OI 界初见于陈丹琦 2008 年的集训队作业中,故被称为CDQ分治. 大致分为三类: cdq分治解决与 ...

  6. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  7. 关于C++指针、引用和const关键字的各种关系

    #include <stdio.h> #include<iostream> using namespace std; typedef char *new_type; int m ...

  8. 专题-集合-ConcurrentHashMap

    本文介绍ConcurrentHashMap是线程安全的,但为什么却不用加锁的原因 一.ConcurrentHashMap简介 在jdk1.7中是采用Segment + HashEntry + Reen ...

  9. Linux新建SVN版本库

    1.进入svn 版本库(如/www/svn) 2.创建svn 版本库svnadmin create  edition 3.进入新建的版本库库目录cd edition 4.进入版本库配置目录cd con ...

  10. Uva 136 丑数

    n^2暴力就完事,但是上限要高,不然就算不到对应的1500,刘汝佳的写法更好. #include <bits/stdc++.h> using namespace std; const in ...