LeetCode--014--最长公共前缀
问题描述:
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
方法1:
贪心:将第一个串和第二个串进行比较,得出的最长前缀再与剩下的进行比较。(48ms)
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs)>1: s0 = strs[0]
s1 = strs[1]
elif len(strs) == 1:
return strs[0]
else:
return ""
common = ""
flag = True
i = 0
while flag and i < len(s0) and i < len(s1):
if s0[i] == s1[i]:
common += s0[i]
else:
flag = False
i += 1
for i in range (2,len(strs)):
c = strs[i]
j = 0
common2 = ""
while j < len(c) and j < len(common):
if c[j] == common[j]:
common2 += c[j]
j += 1
else:
break
common = common2
return common
方法2(官方):
利用min和max函数,找出List中最小值元素s1和最大值元素s2,纪录s1中和s2字符不相同时候的下标,进行截断处理。(24ms)
1 if not len(strs):
return ''
3 s1 = min(strs)
s2 = max(strs)
for n, s in enumerate(s1):
if not s2[n] == s:
return s1[:n]
return s1
注:enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1)) # 小标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
方法3:
ans = ""
for i in zip(*strs):
if len(set(i)) > 1:
return ans
ans += i[0]
return ans
例
strs=["flower","flgdcvghyf","fove"]
for i in zip(*strs):
print(i) >>
('f','f','f')
('l','l','o')
('o','g','v')
('w','d','e')
2018-07-22 11:18:24
LeetCode--014--最长公共前缀的更多相关文章
- LeetCode:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...
- 【LeetCode】最长公共前缀【二分】
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- Java实现 LeetCode 14 最长公共前缀
14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...
- [LeetCode]14.最长公共前缀(Java)
原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...
- python(leetcode)-14最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode 7最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- leetcode 14 最长公共前缀
描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...
- 领扣(LeetCode)最长公共前缀 个人题解
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
随机推荐
- How to install Maven on Windows
To install Apache Maven on Windows, you just need to download the Maven’s zip file, and Unzip it to ...
- topcoder srm 688 div1 -3
1.给出一个只包含'(',')'的字符串$s$,现在对它进行若干次如下操作使其变成匹配的括号串(每次操作包含3个步骤):(1)选择 $L,R,L\leq R$;(2)将$L,R$之间的字符翻转:(3) ...
- FJUT3565 最大公约数之和(容斥)题解
题意:给n,m,求出 思路:题意为求出1~m所有数和n的gcd之和.显然gcd为n的因数.我们都知道gcd(a,b)= c,那么gcd(a/c,b/c)= 1.也就是说我们枚举n所有的因数k,然后去找 ...
- P4390 [BOI2007]Mokia 摩基亚(cdq分治)
一样是cdq的板子 照着园丁的烦恼就好了 代码 #include <cstdio> #include <cstring> #include <algorithm> ...
- (转)Applications of Reinforcement Learning in Real World
Applications of Reinforcement Learning in Real World 2018-08-05 18:58:04 This blog is copied from: h ...
- mysql中创建时间和更新时间的区别
`create_time` ) NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` ) ) COMMENT '更新时间', 而在界 ...
- (转载)Unity里实现更换游戏对象材质球
在unity中本来想实现在一个背景墙上更换图片的功能 在网上查了一些资料说是用Image,但我是新手小白刚接触Unity不久好多组建还不会用,就想能不能通过改变游戏对象的材质球来更换游戏对象的背景. ...
- es索引维护的常用帖子
Elasticsearch 新增字段
- 忘记mysql密码处理方案
https://blog.csdn.net/lisongjia123/article/details/57418989 http://blog.51cto.com/lxsym/477027
- .Net ASP.NET 打开指定文件夹
比如要打开指定的文件夹,而不是弹出对话框 System.Diagnostics.Process.Start(@"D:\"); 这样就打开了D盘,和正常打开D盘是一样的.