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

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

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

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String s = in.nextLine();
            int max = 0;
            //存放数字字符串及它的长度
            Map<StringBuilder,Integer> hm = new LinkedHashMap<>();
            for(int i = 0; i<s.length(); i++){
                int sum = 0;
                StringBuilder sb = new StringBuilder();
                //找到数字字符串
                while(i<s.length() && Character.isDigit(s.charAt(i))){
                    sb.append(s.charAt(i));
                    sum++;
                    i++;
                }
                //保存最大值
                max = Math.max(max,sum);
                if(sum>0){
                    hm.put(sb,sum);
                }
            }
            //遍历哈希表,当value == max时打印
            for(Map.Entry<StringBuilder,Integer> me : hm.entrySet()){
                if(max == me.getValue()){
                    System.out.print(me.getKey().toString());
                }
            }
            //打印字符数
            System.out.println("," + (max));
        }
    }
}
全部评论

相关推荐

07-01 13:37
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
Gaynes:查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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