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]

 

相关文章:

  • 2021-12-12
  • 2022-01-18
  • 2021-08-02
  • 2021-09-25
  • 2022-02-24
  • 2021-08-26
猜你喜欢
  • 2021-11-17
  • 2022-12-23
  • 2021-11-15
  • 2022-03-05
  • 2022-12-23
  • 2022-02-11
相关资源
相似解决方案