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']))
----
全部评论
相关推荐
07-17 15:31
河南师范大学 算法工程师 点赞 评论 收藏
分享