Java 队列 + HashMap

简单错误记录

http://www.nowcoder.com/questionTerminal/2baa6aba39214d6ea91a2e03dff3fbeb

运行时间:10ms
占用内存:9216KB
注意点:

  1. 主要是需要维护队列长度,保证其不超过8。

  2. HashMap用来记录出现过的错误,不能移除

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.Queue;
    public class Main{
     public static void main(String[] args) throws IOException {
         InputStreamReader is = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(is);
         String str = br.readLine();
         Queue<String> queue = new LinkedList<>();
         HashMap<String,Integer> map = new HashMap<>();
         while(str != null) {
             String[] strs = str.split("\\\\");
             String info = strs[strs.length - 1];
             if (map.containsKey(info)) {
                 map.put(info, map.get(info) + 1);
             } else {
                 map.put(info, 1);
                 queue.add(info);
             }
             if(queue.size() > 8){
                 String temp = queue.poll();
                 map.put(temp,0);
             }
             str = br.readLine();
         }
         while(!queue.isEmpty()){
             String temp = queue.poll();
             System.out.println(process(temp)+ " " + map.get(temp));
         }
     }
    
     private static String process(String str){
         String[] strs = str.split(" ");
         int len1 = strs[0].length();
         if(len1 <= 16)
             return str;
         else{
             return strs[0].substring(len1 - 16, len1) + " " +strs[1];
         }
     }
    }
全部评论
题意是第25行不置为0.
点赞 回复 分享
发布于 2021-06-30 00:09

相关推荐

不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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