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

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

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
            String a = in.next();
            int  max = 0;
            String res  = "";
            for (int i = 0 ; i < a.length() - 1; i ++) {
                int  j = i + 1;
                char c = a.charAt(i);
                if (Character.isDigit(c)) {
                    for (; j < a.length() ; j ++) {
                         if (Character.isDigit(a.charAt(j))) {
                         }else{
                            break ;
                         }
                    }
                }
                if(max == j-i){
                    res = res + a.substring(i ,j);
                }
                if( max <j-i){
                    max =  j-i;
                    res = a.substring(i,j);
                }


            }



            System.out.println(res+","+max );
        }
    }
}

全部评论

相关推荐

简历求拷打,海投简历发过去就已读不回了求大佬们指点
程序员牛肉:基本不能了,估计你得放弃秋招,九月份找实习之后明年的春招开始正式找工作
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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