题目:Longest Common Prefix

内容:

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

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

理解题目:

  如数组 ["a", "b"]   最长的公共前缀字符串是 “”;

  如数组 ["aa", "aa"]   最长的公共前缀字符串是 “aa”;

  如数组 ["abc", "abcd"]   最长的公共前缀字符串是 “abc”。。。

解题思路

 1 如果数组为空,则最长公共前缀为"";

 2 如果数组不为空:
(1)找出字符串数组中长度最短的字符串min,其长度为min_len; 因为最长的公共前缀长度不会超过min_len;
(2)遍历数组,将min位置为[i] 的字符 和 数组中其他字符串位置为[i]的字符 比较;
如果不相等,则最长公共前缀为 min的前i个字符;
如果都相等,则最长公共前缀为 min。

Go语言实现

 func longestCommonPrefix(strs []string) string {

     if len(strs) ==  {
return ""
} min_len := len(strs[])
index :=
for i:=; i<len(strs); i++ {
if len(strs[i]) < min_len {
min_len = len(strs[i])
index = i
}
} for j:=; j<len(strs[index]); j++ {
for k:=; k<len(strs); k++ {
if strs[index][j] != strs[k][j]{
return strs[index][:j]
}
}
}
return strs[index]
}

看一下耗时:

Python3实现

 class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
shortstr = min(strs, key=len)
for i, cha in enumerate(shortstr):
for other in strs:
if other[i] != cha:
return shortstr[:i]
return shortstr

看一下耗时:

是不是感觉go要快很多,Python更优雅?

leetcode【14题】Longest Common Prefix的更多相关文章

  1. Leetcode算法刷题:第14题 Longest Common Prefix

    Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...

  2. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  3. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  4. LeetCode 14: Longest Common Prefix

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

  5. 【算法】LeetCode算法题-Longest Common Prefix

    这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...

  6. 乘风破浪:LeetCode真题_014_Longest Common Prefix

    乘风破浪:LeetCode真题_014_Longest Common Prefix 一.前言 如何输出最长的共同前缀呢,在给定的字符串中,我们可以通过笨办法去遍历,直到其中某一个字符不相等了,这样就得 ...

  7. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  8. 【LeetCode算法-14】Longest Common Prefix

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

  9. LeetCodeOJ刷题之14【Longest Common Prefix】

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

  10. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

随机推荐

  1. Java经典编程题50道之二十二

    利用递归方法求5!. public class Example22 {    public static void main(String[] args) {        int n = 5;   ...

  2. Samba服务器的安装与配置

    Samba服务器主要的功能是实现本地windows系统下方便读写局域网内虚拟机下的文件: Samba与window连接需要使用NetBIOS协议,请确认你的Windows系统已经安装了NetBIOS协 ...

  3. python3.6+django2.0 一小时学会开发一套学员管理系统demo

    1.在pycharm中新建project demo1 添加app01 点击create按钮完成新建 2.在demo项目目录下新建目录static,并在settings.py中追加代码: STATICF ...

  4. 如何遍历 Windows 摄像头设备?

    #include <stdlib.h> #include <iostream> #include <Windows.h> #include <comdef.h ...

  5. nyoj720 项目安排 二分+dp

    思路:dp(i)表示前i个项目的最大收益,转移方程很好写dp(i) = max{ dp(k) + val(i) },val(i)表示第i个项目的价值,dp(k)表示前k个的最佳收益,k满足ed(k) ...

  6. CodeForces-731B

    如果当天有m支队伍,昨天选择了k个B方案,那么今天还需要买m-k个披萨,如果m-k是奇数,那就先买一种B,剩下的全部买A,如果是偶数,全部买A.如果中途出现只有0支队伍,然而昨天却买了一次B,那么直接 ...

  7. HDU - 3001 Travelling 状压dp + 三进制 [kuangbin带你飞]专题二

    终于刷完搜索专题了. 题意:给定n个城市,每个城市参观不能超过两次,两个城市之间有道路通过需要花费X,求通过能所有城市的最小花费. 思路:每个城市有三个状态0,1,2,可用三进制存储所有城市的访问状态 ...

  8. Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等

    文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常

  9. 用Postman做自动化测试的功能

    自动化测试应该在桌面应用有该功能,在chrome的插件不知道有没有,我也没装chrome版的Postman Postman工具介绍图 上面这张就是Postman的操作界面.一开始我就是这样做简单的数据 ...

  10. Docker系统六:Docker网络管理

    Docker网络 I. Docer的通信方式 默认情况下,Docker使用网桥(brige)+ NAT的通信模型. Docker启动时会自动创建网桥Docker0,并配置ip 172.17.0.1/1 ...