leetcode Longest Common Prefix python
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) <= 0:
return ""
shortestLen=None
for tmp in strs:
if len(tmp) < shortestLen or shortestLen == None:
shortestLen = len(tmp)
if shortestLen <= 0:
return ""
if len(strs) == 1:
return strs[0]
bolStop=False
res=""
firstStr=strs[0]
del strs[0] for index in range(shortestLen):
strCarry=""
for tmpStr in strs:
strNext = tmpStr[index]
strCur = firstStr[index]
if strNext != strCur:
bolStop=True
break
elif strNext == strCur:
strCarry=strCur
if bolStop:
break
res+=strCarry return res
leetcode Longest Common Prefix python的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- leetcode:Longest Common Prefix【Python版】
1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
随机推荐
- ubuntu系统下设置静态IP
改动 /etc/network/interfaces文件 加入下面内容 # The loopback network interface auto lo eth0 iface lo inet loop ...
- D3学习教程
[ D3.js 入门系列 ] 入门总结 | OUR D3.JS http://www.ourd3js.com/wordpress/?p=396
- “entities.LastOrDefault()”引发了类型“System.NotSupportedException”的异常
问题: var entities = new ShipuPlanBLO().UserList(userId, beginDate, endDate); DateTime maxDate = entit ...
- Csharp实现快速排序
public void QuickSort(int[] arr, int left, int right) //快速排序 { //先从数列中去处一个数作为基准数 //分区过程,将比这个数大的数全放在他 ...
- 斐波那契数列 的两种实现方式(Java)
import java.util.Scanner; /* 斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 如果设F(n)为该数列的第n ...
- BOOST_PP_INC_I(x)实现
这个比较有意思,# define BOOST_PP_INC_I(x) BOOST_PP_INC_ ## x 连接在一起以后,然后定义为x+1 实现了inc功能,不过最多也就到255 # /* Copy ...
- echo向文件中写入
echo命令向一个文件写入内容的方法详解,感兴趣的朋友可以参考下. 覆盖型写法 (文件里原来的内容被覆盖)echo "aaa" > a.txtecho aaa > a. ...
- Linux学习之head命令
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...
- jquery获取和失去焦点改变样式
第一种:(文本框获取焦点后,它的颜色会有所变化,当失去焦点的时候,恢复为原来的样子) <html> <meta http-equiv="Content-Type" ...
- Ajax应用常见的HTTP ContentType设置
文章出处:Ajax应用常见的HTTP ContentType设置 ajax开发中, 常遇到下面的几种情况: 1 服务端需要返回一段普通文本给客户端 2 服务端需要返回一段HTML代码给客户端 3 服务 ...