leetcode:Longest Common Prefix【Python版】
1、当strs为空,直接输出“”
2、当strs中含有“”,直接输出“”
3、strs[0]的最长长度由最短公共长度l决定(code line:15)
class Solution:
# @return a string
def longestCommonPrefix(self, strs):
if strs == []:
return ""
for i in range(1,len(strs)):
l1 = len(strs[0])
l2 = len(strs[i])
if l1>l2:
l = l2
else:
l = l1
if l==0:
return ""
strs[0]=strs[0][0:l]
for j in range(l):
if strs[0][j] != strs[i][j]:
strs[0] = strs[0][0:j]
break
return strs[0]
leetcode:Longest Common Prefix【Python版】的更多相关文章
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
- [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 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
随机推荐
- HDU-4848 Wow! Such Conquering! (回溯+剪枝)
Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super ...
- 新手如何正确使用CLion之输出hello world
以前只使用过vc6.0,在用过jetbrain的pycharm后就考虑换个c++的编译器了,第一还是考虑了vs2017但用惯了色彩鲜艳的jb产品后竟然有点不习惯vs,最后还是果断选择了jb的CLion ...
- HDU 4764 Stone (巴什博弈)
题意 Tang和Jiang玩石子游戏,给定n个石子,每次取[1,k]个石子,最先取完的人失败,Tang先取,问谁是赢家. 思路 比赛的时候想了不久,还WA了一次= =--后来看题解才发现是经典的巴什博 ...
- iOS-UI篇—简单的浏览器查看程序和Tomcat简单实现
#import "ViewController.h" @interface ViewController () @property (retain, nonatomic) NSAr ...
- ES profile 性能优化用——返回各个shard的耗时
Profile API 都说要致富先修路,要调优当然需要先监控啦,elasticsearch在很多层面都提供了stats方便你来监控调优,但是还不够,其实很多情况下查询速度慢很大一部分原因是糟糕的查询 ...
- Inno Setup 编译器操作
Inno Setup 编译器 1◆ 下载inno ha_innosetup5502_skygz_DownG.com 2◆ 安装 next 3◆ 操作 success 4◆ 测试安装 5◆ 卸载 uni ...
- beaglebone-black reference url
reference : https://github.com/beagleboard/beaglebone-black/wiki/System-Reference-Manual https://bea ...
- Linux WiFi Deauthenticated Reason Codes
Code Reason Explanation 0 Reserved Normal working operation 1 Unspecific Reason We don’t know what’s ...
- java代码获取客户端的真实ip
java代码获取客户端的真实ip protected String getIpAddr(HttpServletRequest request) { String ip = request.getHea ...
- 远程访问Centos6.5上的mysql或者mariadb(navicat)
问题背景 1 环境 物理主机操作系统Centos6.5 虚拟主机KVM:centos6.5 64位min版本(虚拟机安装有台) 网络:桥接模式 2 问题 yum安装mariadb10/mysql6.5 ...