非严格递增连续数字序列

标题:非严格递增连续数字序列 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
输入一个字符串仅包含大小写字母和数字,求字符串中包含的最长的非严格递增连续数字序列的长度(比如12234属于非严格递增连续数字序列)。
while True:
    try:
        strs = input().strip()
        i = 0
        pos = 0
        res_list = [0]
        while i < len(strs):
            if strs[i] >= '0' and strs[i] <= '9':
                if i >= 0 and strs[i-1] > strs[i]:
                    pos = i
                res_list.append(i-pos +1)
            i += 1
        print(max(res_list))
    except:
        break
import sys
string = list(sys.stdin.readline().strip())
stack = []
res = 0
for s in string:
    asc = ord(s)
    if 48 <= asc <= 57:
        if stack and stack[-1] <= asc:
            stack.append(asc)
        else:
            res = max(res,len(stack))
            stack = [asc]
    else:
        res = max(res, len(stack))
        stack = []
res = max(res, len(stack))
print(res)



全部评论
def sequence(s): left = 0 right = 1 length = [] mid = 0 while right < len(s) and left < right: if ord(s[right]) - ord(s[left]) == 1 or ord(s[right]) - ord(s[left]) == 0: right += 1 left += 1 else: length.append(right - mid) right = right + 1 left = left + 1 mid = left return max(length) s = input() print(sequence(s))
点赞 回复 分享
发布于 03-09 23:00 广东
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String nextLine = scanner.nextLine(); int left = 0, right = 0, maxLength = 0; while (right < nextLine.length()) { char charAt = nextLine.charAt(right++); //如果字符非数字 if (!Character.isDigit(charAt)) { left = right; continue; } //当前数字字符与前一个数字字符比较 if (right - 2 > 0 && Character.isDigit(nextLine.charAt(right - 2)) && (nextLine.charAt(right - 2) > charAt)) { left = right - 1; continue; } maxLength = Math.max(maxLength, right - left); } System.out.println(maxLength); } }
点赞 回复 分享
发布于 2023-08-03 23:00 湖南
请问这是哪个平台的代码题?
点赞 回复 分享
发布于 2023-01-29 18:43 陕西

相关推荐

群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务