示例1:
输入:["abcde","abcfk","abcgggd"]
输出:"abc"
示例2:
输入:["how","what","when"]
输出:""
Python解决方案:
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if not strs: return "" elif len(strs) == 1: return strs[0] else: out = "" f_len = len(strs[0]) m = 0 while m < f_len: first = strs[0][m] for i in strs[1:]: if m > len(i)-1 or i[m] != first: return out out += i[m] m += 1 return out