Python学习-Leetcode刷题记5:最长公共前缀
2019-02-01 02:00
411 查看
Python学习-Leetcode刷题记5:最长公共前缀
一、问题
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
说明:所有输入只包含小写字母 a-z 。
二、示例
示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。
三、解题过程
class Solution: def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs) == 0: return '' else: L = min([ len(i) for i in strs]) l = len(strs) count = 0 for j in range(L): D = [i[j] for i in strs] if D.count(D[0]) == l: count+=1 else: break return strs[0][:count]
相关文章推荐
- leetcode 14 最长公共前缀(python)
- python [LeetCode] 最长公共前缀
- leetcode_14. 最长公共前缀(python)
- LeetCode14. python实现:最长公共前缀问题☆
- LeetCode题解(python)-14. 最长公共前缀
- leetcode的python实现 刷题笔记14:最长公共前缀
- Leetcode刷题记——14. Longest Common Prefix(最长公共前缀)
- LeetCode:最长公共前缀(Python版本)
- Leetcode题库-最长公共前缀(java语言版)
- 【leetcode数组和字符串】最长公共前缀
- Datawhale-LeetCode集训打卡-最长公共前缀
- leetcode-14-求字符串数组最长公共前缀
- leetcode-最长公共前缀
- Java&LeetCode 初入门——014. 最长公共前缀
- LeetCode14-最长公共前缀
- leetcode-最长公共前缀
- Leetcode 14: 最长公共前缀(Longest Common Prefix)
- LeetCode - 14. 最长公共前缀
- leetcode:java.T014_LongestCommonPrefix--写一个函数找出一个字串所数组中的最长的公共前缀
- Leetcode:Longest Common Prefix 最长公共前缀