共计 398 个字符,预计需要花费 1 分钟才能阅读完成。
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
解法:
class Solution:
def longestCommonPrefix(self, strs: 'List[str]') -> 'str':
if len(strs)<1:
return ''
min_length=min(map(lambda x:len(x),strs))
if min_length<1:
return ''
result=''
for x in range(min_length):
tmp=set([ data[x] for data in strs])
if len(tmp)==1:
result+=strs[0][x]
else:
return result
正文完
请博主喝杯咖啡吧!