全部评论
我第三题的python也是跪在输入输出上了, 有没有大佬通过的呀
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);
}
}
也是跪在输入输出了,哎!
相关推荐
点赞 评论 收藏
分享