D1 - Longest Common Prefix
---
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
short_str = min(strs,key=len)
print(short_str)
for i, char in enumerate(short_str):
for other in strs:
if other[i] != char:
return short_str[:i]
return short_str
#print(Solution().longestCommonPrefix(['abcs','abds']))
---
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
short_str = min(strs,key=len)
print(short_str)
for i, char in enumerate(short_str):
for other in strs:
if other[i] != char:
return short_str[:i]
return short_str
#print(Solution().longestCommonPrefix(['abcs','abds']))
全部评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
飞屋一号:碰到这样的mt可以直接托付终生了
点赞 评论 收藏
分享
查看9道真题和解析