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

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

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


public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.nextLine();
            //加上一个字母,不然最后一个数字字符串检测不到!!!
            s += 'a';
            String cur = "";
            int maxLen = 0;
            ArrayList<String> list = new ArrayList<>();
            for(int i = 0 ; i < s.length(); i++){
                if(s.charAt(i) <= '9' && s.charAt(i) >= '0') {
                    cur += s.charAt(i);
                } else {
                    if(!cur.equals("")) {
                        list.add(new String(cur));
                        maxLen = Math.max(cur.length(), maxLen);
                    }
                    cur = "";
                }
            }
            String res = "";
            for(String str : list) {
                if(str.length() == maxLen) {
                    res += str;
                }
            }
            System.out.println(res + "," + maxLen);
        }
    }
}

``` js
全部评论

相关推荐

01-12 22:27
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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