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

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

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

import java.util.; import java.io.; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = in.readLine()) != null){ int[] num = new int[str.length()]; int sum = 0; int max = 0; for(int i = 0;i < str.length();i++){ if(Character.isDigit(str.charAt(i))){ sum ++; if(max < sum){ max = sum; } num[i] = sum;

            }else{
                sum = 0;
            }
        }
        for(int i = 0;i< num.length;i++){
            if(num[i] == max){
                StringBuilder sb = new StringBuilder();
                for(int j = i - max +1;j <= i;j++){
                    sb.append(str.charAt(j));
                }
                System.out.print(sb.toString());
            }
        }
        System.out.println(","+max);
    }
}

}

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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