首页 > 代码库 > leetcode:Longest Common Prefix【Python版】

leetcode:Longest Common Prefix【Python版】

1、当strs为空,直接输出“”

2、当strs中含有“”,直接输出“”

3、strs[0]的最长长度由最短公共长度l决定(code line:15)

 1 class Solution: 2     # @return a string 3     def longestCommonPrefix(self, strs): 4         if strs == []: 5             return "" 6         for i in range(1,len(strs)): 7             l1 = len(strs[0]) 8             l2 = len(strs[i]) 9             if l1>l2:10                 l = l211             else:12                 l = l113             if l==0:14                 return ""15             strs[0]=strs[0][0:l]16             for j in range(l):17                 if strs[0][j] != strs[i][j]:18                     strs[0] = strs[0][0:j]19                     break20         return strs[0]

 

leetcode:Longest Common Prefix【Python版】