题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map = new LinkedHashMap<>();
        List<String> resList=new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case

            String s = in.next();
            //取行号
            int num = in.nextInt();
            //分隔后取最后一个
            String[] path = s.split("\\\\");
            //取文件名
            String name = path[path.length - 1];
            if (name.length() > 16) {
                //取后16位
                name = name.substring(name.length() - 16, name.length());
            }
            //文件名和行号拼接
            String record = name + " " + num;
            //System.out.println(record);
            //放入HashMap
            Integer count = map.get(record);
            if (count == null) {
                map.put(record, 1);
            } else {
                map.put(record, count + 1);
            }
        }
        //遍历hashMap
        Set<String> set = map.keySet();
        for (String s1 : set) {
            // System.out.println(s1+" "+map.get(s1));
            String res = s1 + " " + map.get(s1);
            resList.add(res);
        }
        int j=0;
        if(resList.size()>8){
            j=resList.size()-8;
        }
        for(int i=j;i<resList.size();i++){
            System.out.println(resList.get(i));
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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