【发布时间】:2020-07-05 15:14:56
【问题描述】:
我正在尝试找出一个简单的 leetcode 问题,但我不知道为什么我的答案不起作用。 问题: 编写一个函数来查找字符串数组中最长的公共前缀字符串。 如果没有公共前缀,则返回一个空字符串“”。 我的代码:
shortest=min(strs,key=len)
strs.remove(shortest)
common=shortest
for i in range(1,len(shortest)):
comparisons=[common in str for str in strs]
if all(comparisons):
print(common)
break
else:
common=common[:-i]
当列表中的字符串长度相同时,上述试验不起作用,但适用于其他情况。 非常感谢。
【问题讨论】:
-
您能提供示例输入和预期输出吗?
标签: python