class Solution:
# @param strs: A list of strings
# @return: The longest common prefix
def longestCommonPrefix(self, strs):
# write your code here
minlen = 99999
for str in strs:
if len(str) < minlen:
minlen = len(str)
if len(strs) == 0:
minlen = 0
for i in range(minlen)[::-1]:
prefixstrs = []
for str in strs:
prefixstrs.append(str[:i + 1])
flag = True
for str in prefixstrs:
if str != prefixstrs[0]:
flag = False
if flag:
return prefixstrs[0]
return ''
[LintCode]Longest Common Prefix(Python)
最新推荐文章于 2021-12-06 18:36:08 发布