题解 | #在字符串中找出连续最长的数字串# 注意边界值

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

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

import java.util.*; 
import java.lang.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String raw  = in.nextLine();
        char[] c = raw.toCharArray();
        List<String> result = new ArrayList<String>();
        StringBuilder s = new StringBuilder();
        int count = 0;
        int max = 0;
        for(int i=0;i<c.length;i++){
            if(Character.isDigit(c[i])){
                s.append(c[i]);
                count++;
            }
            if(!Character.isDigit(c[i])||i==c.length-1){
                if(count>max){
                    result.clear();
                    result.add(s.toString());
                }else if(count == max){
                    result.add(s.toString());
                }
                s.setLength(0);
                max = Math.max(max,count);
                count = 0;
            }
        }
        result.forEach(a->{s.append(a);});
        System.out.println(s+","+max);
    }
}

全部评论

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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