编写一个函数来查找字符串数组中的最长公共前缀,如果不存在返回空字符串“”。
一开始没有思路,但看完答案就懂了,简单题
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
common_lm = strs[0]
for i in range(len(strs)):
common_lm = self.get(common_lm, strs[i])
if common_lm == "":
return ""
return common_lm
def get(self, str1,str2):
i = 0
while(i < len(str1) and i < len(str2) and str1[i] == str2[i]):
i += 1
return str1[:i]