以第一个为标准,依次和后面的进行比较,记录下最长的可以匹配的数目
除此之外,需要进行例外情况处理,字符串列表为空和字符串列表为1;以及循环比较时需注意比较结束为较短字符串的长度。
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:#首先进行特殊情况处理
return ''
if len(strs) == 1:
return strs[0]
temp = strs[0]
count1 = []
for line in strs:#进行遍历是否有空
if line == '':
return ''
for i in range(1,len(strs)):#两两比较,找出最长的
te_count = 0
for j in range(min(len(temp),len(strs[i]))):
if temp[j] == strs[i][j]:
te_count += 1
else:
count1.append(te_count)
break
if te_count == min(len(temp),len(strs[i])):
# print(te_c