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

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

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            String str = in.nextLine();
            List<String> res = new ArrayList<>();
            StringBuilder stringBuilder = new StringBuilder();
            int sum = 0;
            for (char b : str.toCharArray()) {
                if (b >= '0' && b <= '9') {
                    stringBuilder.append(b);
                } else if ((b < '0' || b > '9') && stringBuilder.length() != 0) {
                    res.add(String.valueOf(stringBuilder));
                    sum = Math.max(sum, stringBuilder.length());
                    stringBuilder.setLength(0);
                }
            }
            if (stringBuilder.length() != 0) {
                res.add(String.valueOf(stringBuilder));
                sum = Math.max(sum, stringBuilder.length());
            }
            for (String b : res) {
                if (b.length() == sum)System.out.print(b);
            }
            System.out.println("," + sum);
        }
    }
}

全部评论

相关推荐

10-16 22:56
门头沟学院 C++
1234567800:歌尔今年给211开14-15k吗,我本地人连面试都不给😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务