还是大佬厉害,我的过了50%,提示数组越界 import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int length = input.nextInt(); Node[] list = new Node[length]; int size = 0; int time = 0; HashMap<String, Integer> map = new HashMap<>(); while (input.hasNext()) { String s = input.next(); int num = input.nextInt(); if (map.containsKey(s)) { if (num > list[map.get(s)].num) { list[map.get(s)].num = num; list[map.get(s)].time = time; } } else { if (size < length) { list[size] = new Node(s, num, time); map.put(s, size); size++; } else { Comparator<Node> cmp = new MyComparator(); Arrays.sort(list, cmp); map.remove(list[0].str); System.out.println(list[0].str + " " + list[0].num); list[0] = new Node(s, num, time); map.put(s, 0); for (int i = 1; i < length; i++) { map.put(list[i].str, i); } } } time++; } } static class Node { String str; int num; int time; public Node(String str, int num, int time) { this.str = str; this.num = num; this.time = time; } } static class MyComparator implements Comparator<Node> { @Override public int compare(Node a, Node b) { return a.time < b.time ? -1 : 1; } } }
点赞 4

相关推荐

11-07 11:05
已编辑
西安电子科技大学 golang
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务