最长公共前缀(python) leetcode答案

直接上代码:
def longestCommonPrefix(strs):
"""
:type strs: List[str]
:rtype: str
"""
str_list = strs
if len(str_list) > 1:
s_last = str_list.pop()
common_str = ''
common = ''
for num, str_ in enumerate(str_list):
if num is 0:
if len(s_last) >= len(str_):
len_str = len(str_)
else:
len_str = len(s_last)
for i,j in enumerate(range(len_str)):
if s_last[i] == str_[i]:
common_str += s_last[i]
else:
break
if not common_str:
return ''
else:
if len(common_str) >= len(str_):
len_str = len(str_)
else:
len_str = len(common_str)
for i,j in enumerate(range(len_str)):
if common_str[i] == str_[i]:
common = common_str[:i+1]
else:
common = common_str[:i]
if len(str_list) is 1:
return common_str
else:
return common
if len(str_list) is 1:
return common_str
else:
return common
elif len(str_list) is 1:
return str_list[0]
else:
return ''
最长公共前缀(python) leetcode答案的更多相关文章
- python刷LeetCode:14. 最长公共前缀
难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- python(leetcode)-14最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- 每日一道 LeetCode (5):最长公共前缀
前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- LeetCode(14):最长公共前缀
Easy! 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&qu ...
- LeetCode 14 Longest Common Prefix(最长公共前缀)
题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description Problem: 找出给定的string数组中最 ...
- LeetCode:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
随机推荐
- 对css语法中position值的理解
1.static 正常定位,就是默认定位,根据他的top,right,bottom,left的值 2.relative 根据他top,right,bottom,left的值偏移 3.absolute ...
- orientdb docker-compose 运行
orientdb 很早就跑过,但是现在在跑,发现配置有些变动,原有studio 直接就可以访问的,新版本的居然还需要自己添加 server 的配置,所以为了方便使用docker-compose 运行, ...
- day02 进制之间的转换and计算机编码叙述
一. 进制的转好技巧 二进制:0 1 八进制:0 1 2 3 4 5 6 7 十进制:0 1 2 3 4 5 6 7 8 9 十六进制:0 1 2 3 4 5 6 7 8 9 A B C D E ...
- HanLP vs LTP 分词功能测试
文章摘自github,本次测试选用 HanLP 1.6.0 , LTP 3.4.0 测试思路 使用同一份语料训练两个分词库,同一份测试数据测试两个分词库的性能. 语料库选取1998年01月的人民日报语 ...
- Fibonacci_array
重新开始学习C&C++ Courage is resistance to fear, mastery of fear, not abscence of fear //斐波那契数列 Fibona ...
- catkin_make编译错误
在工作空间下编译ros的代码包,报错如下, CMake Error at LeGO-LOAM/LeGO-LOAM/CMakeLists.txt: (find_package): By not prov ...
- python3-可变长度参数函数(*args 和 **kwargs)
简述: *args: 可变长度元组参数 **kwargs: 可变长度字典参数 ------------------------------------------------------- ...
- C# 6.0:在catch和finally中使用await
Asyn方法是一个现在很常用的方法,当使用async和await时,你或许曾有这样的经历,就是你想要在catch块或finally块中使用它们,比如当出现一个exception而你希望将日志记在文件或 ...
- submit提交判断
body部分 <form action='https://www.baidu.com' method='post' > 用 户 名:<input ...
- Linux安装python3.6
Linux安装python3.6 原地址:https://www.cnblogs.com/kimyeee/p/7250560.html Linux下安装Python3.6和第三方库 如果本机安装 ...