题目
我们现在要实现一个功能找到字符串数组 中的最长公共后缀如果不存在公共后缀。
[“abc” “bbc” c]
输出描述
“c”
示例1:
输入:
[“abc”,“bbc”,“c”]
输出:
“c”
说明:
返回公共后缀: c
示例2:
输入:
[“aa” , “bb”,“cc”]
输出:
“@Zero”
说明:
不存在公共后缀,返回固定结果: @Zero
代码
def findLongestCommonSuffix(strs):
if not strs or len(strs) == 0:
return "@Zero"
common_suffix = strs[0]
for s in strs[1:]:
j = len(common_suffix) - 1
k = len(s) - 1
while j >= 0 and k >= 0 and common_suffix[j] == s[k]:
j -= 1
k -= 1
if j < 0 or k < 0:
common_suffix = common_suffix[j + 1:]
else:
return "@Zero"
return common_suffix if common_suffix else "@Zero"
strs = ["abc", "bbc", "c"]
result = findLongestCommonSuffix(strs)
print(result)
代码展示了如何实现一个函数,查找给定数组中字符串的最长公共后缀,如c在[abc,bbc,c]中,若无公共后缀则返回@Zero
3927

被折叠的 条评论
为什么被折叠?



