题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
思路:
	动态规划
	维护长度为n+1的dp数组
	初始化为0 dp[i]代表以i为结尾的子字符串最长的连续数字串长度
	状态转移方程为:
	dp[index] = dp[index] + 1 if dp[index].isdigit()
代码:
def longestSub(s):
    n = len(s)
    dp = [0] * (n + 1)
    for index in range(n):
        if s[index].isdigit():
            dp[index + 1] = dp[index] + 1
    max_len = max(dp)
    res = ""
    for index in range(n+1):
        if dp[index] == max_len:
            res +=  s[index - dp[index]:index]
    return [res, str(max_len)]
while True:
    try:
        s = input()
        res = longestSub(s)
        print(','.join(res))
    except:
        break
 海康威视公司福利 1137人发布
海康威视公司福利 1137人发布 查看7道真题和解析
查看7道真题和解析