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. 分析 该题目是求一个 ...
随机推荐
- ng组件通讯的几种方式
通过输入型绑定把数据从父组件传到子组件. 如<app-hero-child *ngFor="let hero of heroes" [hero]="hero&qu ...
- 微信小程序初识
http://lib.csdn.net/article/wechat/46742 微信小程序的前途和定位有什么疑惑?点进去 简单先记几个印象名词:流量入口,线下是重点,“即用即走”适合低频工具类产品. ...
- TP3.2 中使用 PHPMailer 发送邮件
第一步.添加PHPMailer类库 http://pan.baidu.com/s/1o7Zc7V0 第二步.添加发送邮件函数 在common目录中的公共函数文件加入函数 <?php /***** ...
- the c programing language 学习过程5
lumped 集成总结 mandating托管 consecutively连续地 contiguous临近的 mnemonic记忆力的 mimics 酷似 魔方 bind捆绑 synonym同义词 s ...
- Spring data mongodb ObjectId ,根据id日期条件查询,省略@CreatedDate注解
先看看ObjectId 的json 结构,非常丰富,这里有唯一机器码,日期,时间戳等等,所以强烈建议ID 使用 ObjectId 类型,并且自带索引 Spring data mongodb 注解 @C ...
- Pymongo一些常见需求(陆续补充)
总结一下最近包括之前遇到的一些pymongo操作的问题. #需求1: 搜索文档数组里边是否存在某元素 数据: data1 = { '_id': xxxxxxxxxxxxxx, 'dataList': ...
- dm642的视频口输出
void VP1_EDMA(int displayMode,unsigned int w,unsigned int h) { unsigned int i=0,k=0; EDMA_Hand ...
- SystemVerilog语言简介(三)
15. 强制类型转换 Verilog不能将一个值强制转换成不同的数据类型.SystemVerilog通过使用'操作符提供了数据类型的强制转换功能.这种强制转换可以转换成任意类型,包括用户定义的类型.例 ...
- eclipse在线安装JBoss Tool过程
eclipse在线安装JBoss Tool过程 1.打开eclipse,依次点击"help--->Install New Software..." 2.再单击"Ad ...
- 从VGA到GPU!细数二十年显卡发展历程
VGA有很多层涵义,本来是用于代表一个分辨率(您可能不了解VGA,但应该知道QVGA代表什么),随后被普遍称为显示输出接口.为了输出VGA分辨 率.提供VGA输出接口,显卡和VGA就有了不解之缘,显卡 ...