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

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

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

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            int maxLen = 0;
            int len = 0;
            //int index = 0;
            int len2 = 0;
            for(int i = 0; i < str.length(); i++){
                if((str.charAt(i)+"").matches("[0-9]")){
                    len++;
                }else if((str.charAt(i)+"").matches("[^0-9]")){
                    if(maxLen < len){
                        maxLen = len;
                        //index = i;
                    }
                    len = 0;
                }else{}
                if(maxLen < len){
                    maxLen = len;
                    //index = i;
                }
            }
            
            for(int i = 0; i < str.length(); i++){
                if((str.charAt(i)+"").matches("[0-9]")){
                    len2++;
                    if(len2 == maxLen){
                        System.out.print(str.substring(i+1-maxLen, i+1));
                        len2 = 0;
                    }
                }else if((str.charAt(i)+"").matches("[^0-9]")){
                    len2 = 0;
                }else{}
            }
            System.out.println(","+maxLen);
        }
    }
}
全部评论

相关推荐

07-16 18:03
门头沟学院 Java
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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