题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#第一遍循环,每找到一个数字,将其索引和长度计入字典。第二遍循环字典,如果循环位置的长度等于最长长度,将其加到字符串sr中。
#优化思路,每找到一个数字,除了上述操作,还可以跳过从此处开始到(索引+长度)后的位置,避免记录无用的长度
while True:
try:dic={}
s=input()
for i in range(len(s)):
if s[i].isdigit():
count=0
for j in range(i,len(s)):
if s[j].isdigit():
count+=1
else:
break
dic[i]=count
sr=''
for i in range(len(dic)):
if list(dic.values())[i]==max(list(dic.values())):
ls1=list(dic.keys())
ls2=list(dic.values())
n=i
for j in range(ls1[i],ls1[i]+ls2[i]):
sr+=s[j]
print(sr+','+str(ls2[n]))
except:
break