Java 队列 + HashMap
简单错误记录
http://www.nowcoder.com/questionTerminal/2baa6aba39214d6ea91a2e03dff3fbeb
运行时间:10ms
占用内存:9216KB
注意点:
主要是需要维护队列长度,保证其不超过8。
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]; } } }