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 ...
随机推荐
- android studio设计模式和文本模式切换
- python --- 05 字典 集合
一.字典 可变数据类型 {key:value}形式 查找效率高 key值必须是不可变的数据类型 1.增删改查 1).增 dic["新key"] = "新va ...
- Flutter基础用法解析
解析开始 Flutter中一切皆widget,一切皆组件.学习Flutter中,必须首先了解Flutter的widget.先从最基本的MaterialApp和Scaffold开始了解 1 Materi ...
- 解决Win10 Git图标不显示问题
升级系统到win10 1803版本以后发现TortoiseGit的忽略图标不显示了 开始以为是版本问题,将TortoiseGit版本进行了升级还是不行 网上查找以后发现 Windows Explore ...
- What is event bubbling and capturing?
What is event bubbling and capturing? 答案1 Event bubbling and capturing are two ways of event propaga ...
- C# 各种控件实现可拖动和调整大小
http://www.360doc.com/content/18/0516/12/55659281_754382494.shtml using System; using System.Collect ...
- 使用vue做表单验证
<template> <Form ref="formInline" :model="formInline" :rules="rule ...
- .NET控件集ComponentOne 2018V3发布:新增图表动画及迷你图
“通过使用 ComponentOne .NET控件产品,实现了兼具 BS 架构灵活性与 CS 架构的客户体验.丰富的控件满足了项目中的各种特殊需求,使得开发的精力可以专注于业务逻辑,为团队节省了时间. ...
- BZOJ 1064: [Noi2008]假面舞会(dfs + 图论好题!)
http://www.lydsy.com/JudgeOnline/problem.php?id=1064 题意: 思路: 考虑以下几种情况: ①无环并且是树: 无环的话就是树结构了,树结构的话想一下就 ...
- HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5919 题意:给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组 ...