class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs)==0:
return ""
if len(strs)==1:
return strs[0]
res = strs[0]
for i in strs[1:]:
res = self.get_same(res, i)
if len(res)==0:
return res
return res
def get_same(self,str_a,str_b):
res = ""
for i,j in zip(str_a,str_b):
if i==j:
res+=i
else:
break
return res
查看了一下比较好的解法,好像思路都一样,zip用的比较好哈哈哈哈
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
result = ""
for temp in zip(*strs):
if len(set(temp)) == 1:
result += temp[0]
else:
break
return result
其实思路都是一样的···