1. 程式人生 > >【leetcode】最長公共子字首 python3(擊敗99.65%使用者)

【leetcode】最長公共子字首 python3(擊敗99.65%使用者)

【leetcode】最長公共子字首 python3(擊敗99.65%使用者)

#解題分析

列表共有三種情況:
1、列表為空,即[],len(strs) ==0,返回 ’ ’
2、列表中有為’ ’ 的元素,返回 ’ ’
3、正常情況

class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0 or '' in
strs: return '' if len(strs) == 1: return strs[0] else: s1 = min(strs) s2 = max(strs) s = '' for k,v in enumerate(s1): if s2[k] == v: s += v else: break
return s