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

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

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


# 这是纯应用题目
# 判断数字,然后长度判断

def get_str(str_list):
    num_list = ['0','1','2','3','4','5','6','7','8','9']
    str_list.append('a')
    path = []
    result = []
    for i in range(len(str_list)):
        if str_list[i] in num_list:
            path.append(str_list[i])
        if str_list[i] not in num_list and str_list[i-1] in num_list:
            result.append(path)
            path = []
    # print(result)
    # 进行比较和输出
    num = 0
    for i in range(len(result)):
        if len(result[i]) > num:
            num = len(result[i])
            res = result[i]

        elif len(result[i]) == num:
            res = res + result[i]

    print("".join(res)+','+str(num))


while True:
    try:
        str_list = list(input())
        get_str(str_list)

    except:
        break






全部评论

相关推荐

2024-12-30 22:31
吉首大学 Web前端
工字钢写代码:改成吉林就OK了
点赞 评论 收藏
分享
kl_我是东山啊:《相关公司:阿里巴巴》
投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务