Longest Common Prefix (Python)
kitt
posted @ 2014年1月30日 01:13
in LeetCode
, 2952 阅读
class Solution: # @return a string def longestCommonPrefix(self, strs): len_strs = len(strs) if len_strs == 0: return '' if len_strs == 1: return strs[0] lengths = [len(i) for i in strs] common_prefix = '' for i in xrange(min(lengths)): tmp = strs[0][i] for j in xrange(1,len_strs): if strs[j][i] != tmp: return common_prefix common_prefix += tmp return common_prefix