题解 | #简单错误记录#

简单错误记录

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

map + list 或者LinkedHashMap

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        /**
        1. 使用set 保证单次出现
        2.使用list 记录顺序
         */
        Map<String, Integer> map = new HashMap<>();
        List<String> list = new  ArrayList<>();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            String[] s1 = str.split(" ");
            String[] s2 = s1[0].split("\\\\");
            String fileName=s2[s2.length - 1];
            if(fileName.length()>16){
                fileName=fileName.substring(fileName.length()-16);
            }
            String  temp = fileName + " " + s1[1];
            if (!map.containsKey(temp)) {
                map.put(temp, 1);
                list.add(temp);
            } else {
                map.put(temp, map.get(temp) + 1);
            }

            
        }
        int start = 0;
        if (list.size() > 8) {
            start = list.size() - 8;
        }

        for (int i = start; i < list.size(); i++) {
            System.out.println(list.get(i) + " " + map.get(list.get(i)));
        }

    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务