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

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

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);
        }
    }
}

全部评论

相关推荐

手撕没做出来是不是一定挂
Chrispp3:不会,写出来也不一定过
点赞 评论 收藏
分享
11-13 20:32
门头沟学院 Java
面向未来编程code:我没看到他咋急,他不就问你个问题。。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务