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:

  1. Input: ["flower","flow","flight"]
  2. Output: "fl"

Example 2:

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

Note:

All given inputs are in lowercase letters a-z.

2、我的解法

  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/1/29 12:28
  3. # @Author : SmartCat0929
  4. # @Email : 1027699719@qq.com
  5. # @Link : https://github.com/SmartCat0929
  6. # @Site :
  7. # @File : 14. Longest Common Prefix.py
  8.  
  9. from typing import List
  10.  
  11. class Solution:
  12. def longestCommonPrefix(self, strs: List[str]) -> str:
  13. d = len(strs)
  14. minLength = 999999
  15. for str in strs:
  16. thisLength = len(str)
  17. if thisLength < minLength:
  18. minLength = thisLength
  19. res = ""
  20. if d > 0:
  21. for i in range(minLength):
  22. n = 0
  23. r = strs[0][i]
  24. for j in range(d):
  25. if r == strs[j][i]:
  26. n = n + 1
  27. if n == d:
  28. break
  29. else:
  30. return res
  31. res = res + r
  32. return res
  33. else:
  34. return res
  35.  
  36. # print(Solution().longestCommonPrefix(["flower", "flow", "flight"]))
  37. 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. Python,正则表达式 - (?:)示例

    例如正则表达式a(?:b),匹配后没有包含'b'的分组 >>> string 'ab ac' >>> import re >>> string = ...

  2. 了解 go 的 Context

    go 的 Context 一直对 go 的 Context 一知半解,不了解其用途,因此在这里着重了解一下 go 语言的 Context 飞雪无情的一个博文对 go 的 Context 讲的比较易懂一 ...

  3. Python(一):一行解法参考

    #一行快速排序quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in arra ...

  4. VMware 搭建linux虚拟机环境

    1.任务管理器-服务 确认VMware服务是否启动 2.VMware生成网关地址 编辑--虚拟网络编辑器 VMnet8 NAT设置子网IP,子网掩码,网关 3.windows网络--更改适配器设置-- ...

  5. vue+vuex项目中怎么实现input模糊查询

    1,首先给input框添加方法,但是用的是element-ui的组件,对input进行了封装,不能直接用原生的方法!,在element组件中,input框中方法有实例参数$event,代表事件对象  ...

  6. Ubuntu中获取和使用uiautomatorviewer

    图省事的办法是直接在网上找个android platform tools的某个版本下载下来 比如这个网站里面的:Android SDK工具——Platform-tools 个人比较倾向于先下载andr ...

  7. Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)

    DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把 ...

  8. Go_栈

    1. 栈的介绍 2. 栈的应用 3. 栈入门 package main import ( "fmt" "errors" ) //使用数组来模拟一个栈的使用 ty ...

  9. 统计字符在字符串中第n次出现的位置

    输入一个字符串s,一个数字n和一个字符c,统计这个字符c在字符串s中第n次出现的位置 输入格式: 输入3行.第1行是字符串s,第2行是数字n,第3行是被查找的字符c. 输出格式: 第n个字符在字符串中 ...

  10. C++记录(一)

    1 extern 符表示该变量不是当前作用域定义的,用于声明. 如extern i;表示i不是当前作用域里的,是其他某个include的cpp文件里的变量. 2 int *p=0;相当于初始化p为空指 ...