14,最长公共前缀

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        rlt = ''
        test_list = []
        pos = 0
        while True:
            try:
                for s in strs:
                    test_list.append(s[pos])
                if len(test_list) == len(strs) and len(set(test_list)) == 1:
                    rlt += strs[0][pos]
                    pos += 1
                    test_list = []
                else:
                    break
            except Exception:
                break
        return rlt

猜你喜欢

转载自blog.csdn.net/weixin_42758299/article/details/88551847