题解 | #在字符串中找出连续最长的数字串#正则提取判断最大

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

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

import re
while True:
    try:
        dstr = input()
        number = re.findall("\d+",dstr)
        maxlen = 0
        maxstr = ""
        for i in number:
            if len(i)>maxlen:
                maxstr = i
                maxlen = len(i)
            elif len(i)==maxlen:
                maxstr  = maxstr+i
        print (maxstr+","+str(maxlen))
    except:
        break





# while True:
#     try:
#         dstr = input()
#         for i in dstr:
#             if not i.isdigit():
#                 dstr = dstr.replace(i," ")
#         maxnum = 1
#         maxstr = ""
#         for i in dstr.split(" "):
#             if len(i)>maxnum:
#                 maxnum = len(i)
#                 maxstr = i
#             elif len(i)==maxnum:
#                 maxstr = maxstr+i
#         if maxstr == "":
#             print (",0")
#         else:
#             print(maxstr+","+str(maxnum))
#     except:
#         break




# # def maxlen(dstr,i,maxnum):
# #     j = i+1
# #     while j<maxnum:
# #         if dstr[i:j+1].isdigit():
# #             j=j+1
# #         else:
# #             return dstr[i:j],j
# #     return dstr[i:j],j       
# # while True:
# #     try:
# #         dstr = input()
# #         sdict = {}
# #         maxnum = len(dstr)
# #         i = 0 
# #         maxlen = 0
# #         maxstr = ""
# #         while i  <maxnum:
# #             if dstr[i].isdigit():
# #                 nstr,i = maxlen(dstr,i,maxnum)
# #                 if len(nstr) >maxlen:
# #                     maxlen = len(nstr)
# #                     maxstr = nstr
# #                 elif len(nstr) == maxlen:
# #                     maxstr = maxstr+nstr
# #             else:
# #                 i = i+1
# #         print (maxstr+","+str(maxlen))
# #     except:
# #         break

全部评论
楼主社招算法工程师成功了吗
点赞 回复 分享
发布于 2023-11-17 22:43 广东

相关推荐

牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务