package bianlifeng; import java.util.LinkedHashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); LRUCache lruCache4 = new LRUCache(n); String str; while ((str = sc.nextLine()).contains(" ")){ String[] split = str.split(" "); int key = Integer.parseInt(split[0]); int value = Integer.parseInt(split[1]); lruCache4.put(key, value); } int key = Integer.parseInt(str); System.out.println(lruCache4.get(key)); } } class LRUCache { public int capacity; public LinkedHashMap<Integer, Integer> map; public LRUCache(int capacity) { this.capacity = capacity; map = new LinkedHashMap<>(); } public int get(int key) { if (map.containsKey(key)) { int value = map.get(key); map.remove(key); map.put(key, value); return value; } else { return -1; } } public void put(int key, int value) { if (map.containsKey(key)) { map.remove(key); } if (map.size() >= capacity) { map.remove(map.keySet().iterator().next()); } map.put(key, value); } }
点赞 1

相关推荐

不愿透露姓名的神秘牛友
05-25 18:29
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务