Easy
, Array/String
确定一个string列里各个string的最长共同前缀
各个string并列后从左到右对比,如果同一位值的字符相同,加入响应,否则返回。这里到了set()来确定是否相同。
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
pre = ''
i = 0
while True:
str_lens = [len(str) for str in strs]
if all(len>i for len in str_lens):
com = set([str[i] for str in strs])
if len(com) == 1:
pre += list(com)[0]
else:
break
else:
break
i += 1
return pre