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 ...
随机推荐
- 单用户模式破解root密码
- django基础 -- 4. 模板语言 过滤器 模板继承 FBV 和CBV 装饰器 组件
一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用 {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...
- topcoder srm 708 div1 -3
1.定义一个字符串s,定义函数$f(s)=\sum_{i=1}^{i<|s|}[s_{i-1}\neq s_{i}]$,给定字符串$p,q$,定义函数$g(p,q)=\sum_{c='a'}^{ ...
- s2-045漏洞批量检测工具
今天晚上看老铁们在群里就这个st2-045漏洞讨论得火热,个人不太喜欢日站,本来想直接写个批量挂马的东西,但是想想还是算了,如果你有兴趣,改改也很容易,反正不关我的事 测试图 2017-3-8更新 增 ...
- Excel lastindex of a substring
I think I get what you mean. Let's say for example you want the right-most \ in the following string ...
- (转) AdversarialNetsPapers
本文转自:https://github.com/zhangqianhui/AdversarialNetsPapers AdversarialNetsPapers The classical Pap ...
- Yarn 的日志聚集功能配置使用
需要 hadoop 的安装目录/etc/hadoop/yarn-site.xml 中进行配置 配置内容 <property> <name>yarn.log-aggregati ...
- Async、Await
Async.Await:net4.x新增的异步编程方式: 目的:为了简化异步程序编写 Async方式, 使用Async标记Async1为异步方法, 用Await标记GetRequestStreamAs ...
- Angular CLI命令
ng 基础命令 npm install –g @angular/cli npm install -g @angular/cli@latest ng serve –prot –aot 启动项目并压缩项目 ...
- java web 工程更改名字
如图: 将工程名字struts2Project02更改为struts2Project03,步骤如下: 1. 右键工程名字,选中properties,如图 2.更改项目名字 3.第2步已经真正把项目名字 ...