代码实现
直观的做法
class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:end=1number=len(strs)if number==0:# 排除空列表的情况return ""minimal_length=100for i in range(number):# 找到列表中最小的字符串长度t=len(strs[i])if t<minimal_length:minimal_length=tif minimal_length==0: # 排除不是空列表但是列表中字符串都为空的情况return ""key=0# 用于跳出循环for end in range(1,minimal_length+1):# end是切片参数t=strs[0][:end]# 尝试切片得到一种后缀for i in range(number):if t==strs[i][:end]:continueelse:end=end-1key=1breakif key==1:breakreturn strs[0][:end]
Python版[leetcode]14. 最长公共前缀(难度简单) # 有好几种方法,但是没看懂
Python zip()与zip(*)区别
Python中map与lambda的结合使用
