题目:
Write a function to find the longest common prefix string amongst an array of strings.
代码:
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if strs==None or len(strs)==0:
return ''
res = strs[0]
for str in strs:
i = 0
while i<min(len(res),len(str)) and res[i]==str[i]:
i += 1
if i == 0:
return ''
else:
res = res[:i]
return res
网友评论