lc14-Longest Common Prefix 發表於 2016-05-08 | 分類於 leetcode | 0 comments | 123456789101112131415161718192021222324class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs) == 0: return "" leng=len(strs[0]) shortest_index=0 if len(strs) == 1: return strs[0] #find shortest for i,str in enumerate(strs): if len(str)<leng: shortest_index=i leng=len(str) pref=strs[shortest_index] #find most common prefix for str in strs: while str.startswith(pref) == False: leng-=1 pref=pref[:leng] return pref