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 = l2
11             else:
12                 l = l1
13             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                     break
20         return strs[0]

 

posted @ 2014-10-18 23:19  ZH奶酪  阅读(1764)  评论(0编辑  收藏  举报