题目来源:

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的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 【LeetCode】014. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...

  3. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  4. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  5. No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...

  6. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  7. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  8. LeetCode--No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...

  9. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

  10. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

随机推荐

  1. 《windows程序设计》学习_3.4:实现雷区翻转

    #include<windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPAR ...

  2. nmon related

    nmon related pGraph (supports nmon) https://www.ibm.com/developerworks/community/wikis/home?lang=en# ...

  3. g711u与g729比較编码格式

    •711a-编解码格式为G.711 alaw •g711u-编解码格式为G.711 ulaw (the default) •g729-编解码格式为G.729 •g729a-编解码格式为G.729a 上 ...

  4. tlplayer for ios V1.0

    此程序UI修改于虎跃在线课堂.所以极其相似. 可以播放网络视频与本地视频,不知道怎么拷贝本地视频到Ipad或iphone上看的朋友,请自己到网上看教程. 支持mms,file,rtsp,rtmp,ht ...

  5. spring AOP简单入门

    AOP(aspect oriented programming)面向切面编程. 大致意思是在方法的执行过程中织入其他要执行的方法. 项目结构图 先介绍一下通过代理的方式实现aop,几个文件和上一篇一样 ...

  6. C语言的指针变量

    C语言的指针变量 在C语言中,变量是固定范围的存储空间,它存储的是赋给他的值, 比如: ; /* 这里是定义一个整型变量a,并把12这个值存储在a的地址空间上 这个地址空间是系统随机分配的,对用户是透 ...

  7. js正则语法

    整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数 ...

  8. python第一步

    安装2.7的python 环境:到cmd下python,就可以跑代码了,要是想运行py文件,在命令行python test.py,记得在windows下把python加入环境变量 学习基础的语法: 注 ...

  9. HDU 2104 hide handkerchief

    题解:由题目可以知道,如果n和m的最大公约数不为1,那么总有箱子是无法遍历的,所以求一遍GCD就可以判断了. 注意点:一定要记住判断是==,在做题时又忘了. #include <cstdio&g ...

  10. HDU1181【有向图的传递闭包】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意很简单. 有用并查集做的.我这里用传递闭包做. 有向图的传递闭包采用Floyd思想,可以判断 ...