[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. 分析 该题目是求一个 ...
随机推荐
- ios蓝牙开发(四)BabyBluetooth蓝牙库
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- 关于js封装框架类库之样式操作
在js中,对样式的操作我们并不感到陌生,在很多框架中都是用极少的代码,实现更强大的功能,在这做出一些的总结.存在不足还望指出! 1.封装一个添加css的方法(这篇引用了前面的框架结构) 在 js 中 ...
- 用 rsync 同步本地和服务器的文件
参考 DigitalOcean 安装 For Debian/Ubuntu: sudo apt-get install rsync For OpenSUSE/Fedora: sudo yum insta ...
- iOS面试题04-runtime
runtime/KVO等面试题 1.KVO内部实现原则 回答:1>KVO是基于runtime机制实现的 2>当某个类的对象第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个 ...
- docNet基础学完感想
开学后的一个多月因为要准备acm省赛,所以docnet视频基本没看了!不过,虽然在省赛前每天都在做题,赛前刷了80多题吧!!但是比赛的时候就3题,渣啊!只做出了3个水题,后面两个小时搞两题就是出不来, ...
- [Jobdu] 题目1385:重建二叉树
根据一棵二叉树的先序遍历和后序遍历,重建二叉树 例子: 我们先来看一个例子,二叉树如上图,则先序遍历为:1 2 4 7 3 5 6 8,中序遍历为:4 7 2 1 5 3 8 6 思路: 先序遍历中的 ...
- C语言格式化输出,空位补0,空位补空格
char strTtimeDump[512] = ""; int a = 5; sprintf(strTtimeDump, "%.4d", a); //strT ...
- Hadoop学习笔记(3)hadoop伪分布模式安装
为了学习这部分的功能,我们这里的linux都是使用root用户登录的.所以每个命令的前面都有一个#符号. 伪分布模式安装步骤: 关闭防火墙 修改ip地址 修改hostname 设置ssh自动登录 安装 ...
- 两种写法实现Session Scope的Spring Bean
xml based: <bean id="localRepository" class="com.demo.bean.LocalRepository" s ...
- 高质量程序设计指南C/C++语言——C++/C常量(2)