leetcode【14题】Longest Common Prefix
题目: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的更多相关文章
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- LeetCode 14: Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【算法】LeetCode算法题-Longest Common Prefix
这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...
- 乘风破浪:LeetCode真题_014_Longest Common Prefix
乘风破浪:LeetCode真题_014_Longest Common Prefix 一.前言 如何输出最长的共同前缀呢,在给定的字符串中,我们可以通过笨办法去遍历,直到其中某一个字符不相等了,这样就得 ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
随机推荐
- ios音乐播放器demo
闲暇时间,写了一个音乐播放器. 个人认为,基于Demo 的学习是最有效果的. 想学习的同学,欢迎下载.知识,只有在传播的时候才有价值. 不懂之处,欢迎留言询问,将热情解答. 运行图 项目结构图 Git ...
- CodeForces-748B
关键在于判断是否能够得到解决办法,我的思路就是用一个数组来记录每个小写字母对应的按键,如果它出现对应两个级以上不同的按键那么就说明不能得出解决办法,直接打印'-1'.如果能够得出解决办法,就扫描一下数 ...
- UVA - 242 线性DP
题意:给定多种邮票的组合,邮票最多只能用S张,这些邮票能组成许多不同面额,问最大连续面额的长度是多少,如果有多个组合输出组合中邮票数量最少的,如果仍有长度一致的,输出邮票从大到小排序后字典序最大的那个 ...
- 关于xlrd处理合并单元格
先埋个雷, 最近在做通过excel读取接口测试用例~ 流程等都是自己制定的,打算做完了之后放到GitHub上去哈哈哈. 正式进入正题~ 在写这个框架的时候,遇到了一个问题,就是同一个接口,需要为他准备 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 简介 这是一篇关于Redis使用的总结类型文章,会先简单的谈一下缓存 ...
- python语言中的AOP利器:装饰器
一.前言 面向切面编程(AOP)是一种编程思想,与OOP并不矛盾,只是它们的关注点相同.面向对象的目的在于抽象和管理,而面向切面的目的在于解耦和复用. 举两个大家都接触过的AOP的例子: 1)java ...
- Error Code: 1414. OUT or INOUT argument 2 for routine company.new_procedure is not a variable or NEW
1.错误描述 16:27:36 call new_procedure(20150112,1) Error Code: 1414. OUT or INOUT argument 2 for routine ...
- MySQL日期类型和毫秒值相互转换
有时需要将日期类型值转换成毫秒值,有时也要将毫秒值转换成日期,为了更方便,满足查询的需要. 现在,新建一张数据库表t_stu_info,并向表里插入数据 use test; show tables; ...
- ATCA构架
ATCA(Advanced Telecom Computing Architecture)标准即先进的电信计算平台,它脱胎于在电信.航天.工业控制.医疗器械.智能交通.军事装备等领域应用广泛的新一代主 ...
- BUILD FAILED D:\build.xml:2: 前言中不允许有内容。
1.错误描述 Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administ ...