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

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

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int maxLen = 0;
            String s = in.next();
            s = s.replaceAll("[^0-9]"," ");
            s = s.replaceAll("\\s+"," ");
            String[] ss = s.split(" ");
            for(int i=0; i<ss.length; i++){
                if(ss[i].length() >= maxLen){
                    maxLen = ss[i].length();
                }
            }
            for(int i=0; i<ss.length; i++){
                if(ss[i].length() == maxLen){
                    System.out.print(ss[i]);
                }
            }
            System.out.println("," + maxLen);
        }
    }
}

正则表达式两步替换,简单实现

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 15:36
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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