题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
import java.util.Scanner;
import java.util.*;
public class Main{
//用array list顺序表去做统计
// 独立一个方法去取值key错误记录
//用 map1 去记录 key错误记录在顺序表中的位置
//用 map2 去判断并记录 key错误记录出现的次数
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] keySplit = null;
String[] nameSplit = null;
List<String> outList = new ArrayList<>();
Map<String, Integer> occurTimesMap = new HashMap<>();
String key = null;
while (in.hasNextLine()) {
String input = in.nextLine();
if (input != null && "" != input) {
keySplit = input.split("\\\\");
} else {
break;
}
if (keySplit != null) {
nameSplit = keySplit[keySplit.length - 1].split(" ");
}
//取file name值
if (nameSplit != null) {
if (nameSplit[0].length() <= 16) {
key = keySplit[keySplit.length - 1];
} else {
key = nameSplit[0].substring(nameSplit[0].length() - 16) + " " + nameSplit[1];
}
}
//如果复现,累计出现次数
if (occurTimesMap.get(key) != null) {
occurTimesMap.put(key, occurTimesMap.get(key) + 1);
} else {
//统计第一次,并且添加list
occurTimesMap.put(key, 1);
outList.add(key);
}
}
//取list最后8条+对应的次数值
for (int i = outList.size() > 8 ? outList.size() - 8 : 0; i < outList.size();
i++) {
System.out.println(outList.get(i) + " " + occurTimesMap.get(outList.get(i)));
}
}
}

查看24道真题和解析