题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

cc = input()

leng = len(cc)
dp = [0 for _ in range(leng)]

if cc[0].isdigit():
    dp[0] = 1
else:
    dp[0] = 0

for j in range(1, leng):
    if cc[j-1].isdigit() and cc[j].isdigit():
        dp[j] = dp[j-1] + 1
    elif not cc[j-1].isdigit() and cc[j].isdigit():
        dp[j] = 1
    elif not cc[j].isdigit():
        dp[j] = 0

max_num = max(dp)

all_index = []
for index, item in enumerate(dp):
    if item == max_num:
        all_index.append(index)

# print(dp)
# print(all_index)

number = ''
for index in all_index:
    number = number + cc[index-max_num+1:index+1]
print(number + ',' + str(max_num))

全部评论

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务