最长公共前缀(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",& ...
随机推荐
- C语言练习题库----数组
有如下语句 int a[10] = {1,2,3,4,5,6,7,8,9,10};int *p = a;则数值为9的表达式是______ *p+9 b) ...
- Git 概念
Git 概念 一.Git 工作流程 ~ Workspace:工作区 ~ Index/ Stage:暂存区 ~ Repository:仓库区(或本地仓库) ~ Remote:远程仓库 工作区 进行开发改 ...
- ftruncate
普通文件或共享内存区的大小都可以通过该函数修改 #include <unistd.h> int ftruncate(int fd,off_t leght); //成功返回0失败返回-1 对 ...
- 16.2 在SecureCRT编写C程序不高亮显示
打开“会话选项”>在类别里找“终端”>选择“仿真”>终端的下拉里选择“Xtrem”>在“ANSI颜色”里打钩,在“使用颜色方案”打钩
- python selenium 处理时间日期控件(十六)
测试过程中经常遇到时间控件,需要我们来选择日期,一般处理时间控件通过层级定位来操作或者通过调用js来实现. 1.首先我们看一下如何通过层级定位来操作时间控件. 通过示例图可以看到,日期控件是无法输入日 ...
- docker-solr 使用host模式修改默认的8983端口号
1.使用root账号进入docker-solr docker exec -it 127627ab6247 -u root /bin/bash 2.安装vim apt-get update apt-ge ...
- 关于微信小程序获取二维码的踩坑记录
1.踩坑需求:获取小程序的二维码 2.踩坑接口: https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN3 踩坑代码 pu ...
- Oracle 动态SQL 注意细节 ORA-00911: 无效字符
随笔 - 46 文章 - 92 评论 - 5 lv_sql:=' insert into ETL_SUCESS_AMOUNT select SEQ_OS_ETL_AMOUNTID.NEXT ...
- php7 pdo抽象类操作数据库
查询 <?php try { $dbconnect = new PDO('mysql:host=localhost;dbname=pdodatabase','root','753951'); } ...
- C# .NET newtonsoft.json 多版本冲突解决
A.DLL 引用了6.0 的 newtonsoft.json (V2 运行时),B.DLL 引用了10.0 的 newtonsoft.json (V4 运行时). 可以在.CONFIG RUNTIM ...