[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. 分析 该题目是求一个 ...
随机推荐
- 化简复杂逻辑,编写紧凑的if条件语句(二):依据if子句顺序化简条件
<化简复杂逻辑,编写紧凑的if条件语句>已经得出了跳.等.飞.异常的各自条件,方便起见这里重新贴一下. 立即跃迁:!a && b && d 等待跃迁:!a ...
- 阻止长按复制页面中的内容;zepto中touch中的应用必须先加载event模块之后;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle 生成指定范围内随机日期
Oracle生成一个指定范围内的随机日期 /* 年1月1日)的整数偏移量来保存(即把日期保存为一个数字); * 因此可通过寻找‘指定日期’与‘关键日期’相对应的整数偏移量,再加一个指定范围内的随机整数 ...
- 我的IOS学习之路(三):手势识别器
在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...
- Android应用开发基础篇(6)-----Service
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/20/2360336.html 一.概述 我们知道,Service是Android的四大组件之一. ...
- C++重载赋值运算符
这是一道C++的面试题,下面在这篇博客中分析一下这个问题.先上题目: //题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: ...
- The introduction to Web.config of ASP.NET #Reprinted#
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...
- 射频识别技术漫谈(16)——Mifare UltraLight
Mifare UltraLight又称为MF0,从UltraLight(超轻的)这个名字就可以看出来,它是一个低成本.小容量的卡片.低成本,是指它是目前市场中价格最低的遵守ISO14443A协议的芯片 ...
- 【Untiy3D 游戏开发之一】Unity3D For Window/Mac最新4.2.0版本破解教程
转载请标明:转载自[小枫栏目],博文链接:http://blog.csdn.net/rexuefengye/article/details/11646885 一.Unity3D For Mac 1.首 ...
- JavaEE Tutorials (10) - Java持久化查询语言
10.1查询语言术语14010.2使用Java持久化查询语言创建查询141 10.2.1查询中的命名参数142 10.2.2查询中的位置参数14210.3简化的查询语言语法142 10.3.1选择语句 ...