[LeetCode]题解(python):014-Longest Common Prefix
题目来源:
https://leetcode.com/problems/longest-common-prefix/
题意分析:
这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串。
题目思路:
这都题目的难度是简单。从字符串头部开始计算,初始化公共前缀子字符串是strs[0],公共子字符串每和下一个字符串得到一个新的最新公共前缀子字符串,直到公共前缀子字符串是空或者比较到了最后一个字符串。
代码(python):
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
size = len(strs)
if size == 0:
return ''
if size == 1:
return strs[0]
ans = strs[0]
i = 1
while i < size:
j = 0
minlen = min(len(ans),len(strs[i]))
#print(minlen)
while j < minlen:
if ans[j] != strs[i][j]:
break
j += 1
if j == 0:
return ''
ans = ans[:j]
i += 1
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4818912.html
[LeetCode]题解(python):014-Longest Common Prefix的更多相关文章
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】014. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- 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
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode--No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
随机推荐
- zookeeper 伪集群模式
问题二:开发没有足够机器,一台机子上是否装三个zookeeper服务器集群. 问题解答: 这种安装模式只能说是一种伪集群模式.三个zookeeper服务器都安装在同一个服务器(platform)上,需 ...
- 如何成为uber司机,uber司机详细注册流程
怎样注册uber司机 如何注册加入uber司机 全国加入Uber 的要求 车辆要求:要求裸车价8万以上,车龄5年以内,第三者责任险保额30万以上,不支持20万以下的面包车/商务车,不支持4座以下车辆. ...
- Cube Stacking(并差集深度+结点个数)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21567 Accepted: 7554 Ca ...
- g711u与g729比較编码格式
•711a-编解码格式为G.711 alaw •g711u-编解码格式为G.711 ulaw (the default) •g729-编解码格式为G.729 •g729a-编解码格式为G.729a 上 ...
- 不允许在单例对象中创建Srping容器
spring.net在使用的时候,不允许在单例对象中创建Srping容器 需要将实例化模式转为单例singleton=“false”
- c/c++ double的数字 转成字符串后 可以有效的避免精度要求不高的数
char n[100]; sprintf(n,"%lf",db);
- objective-C学习笔记(十一)类别和扩展
类别 类别是对外的,外部都可以访问 类别是在没有源代码或者基于某些特定场合的情况下,为一个类增加功能(方法).或者用于给一个特别大的类进行分割. 命名规则:类名+扩展方法,如NSString 可以添加 ...
- VS2015 启用“仅我的代码”
在调试网站的时候,如果不勾选 [启用"仅我的代码"],会跳出一大堆异常,但是异常不作处理,非常烦人: 解决办法就是在 [调试]->[选项]->[勾选 启用"仅 ...
- bzoj 1007 : [HNOI2008]水平可见直线 计算几何
题目链接 给出n条直线, 问从y轴上方向下看, 能看到哪些直线, 输出这些直线的编号. 首先我们按斜率排序, 然后依次加入一个栈里面, 如果刚加入的直线, 和之前的那条直线斜率相等, 那么显然之前的会 ...
- QT不让windows休眠的方法
对于一些Windows应用程序,必须要保证os不能休眠才能有效工作,如迅雷下载软件,如果os进入休眠,则会导致网络不正常,从而导致不能下载东西.那木有没有1种机制,当打开软件的时候,就自动将os设为不 ...