import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param operations string字符串一维数组 * @param data int整型二维数组 * @param capacity int整型 * @return int整型一维数组 */ public int[] performOperation (String[] operations, int[][] data, int capacity) { // write code here TicketCache ticketCache = new TicketCache(capacity); List<Integer> returnL = new ArrayList<>(); for (int i=0; i < operations.length; i++){ if (operations[i].equals("putTicket")){ ticketCache.putTicket(data[i][0], data[i][1]); }else if (operations[i].equals("getTicket")){ int info = ticketCache.getTicket(data[i][0]); returnL.add(info); } } int[] returnA = returnL.stream().mapToInt(Integer::valueOf).toArray(); return returnA; } public class TicketCache{ int capacity; Map<Integer, Integer> tickectInfoMap; LinkedList<Integer> linkedL; public TicketCache(int capacity){ this.capacity = capacity; this.tickectInfoMap = new HashMap<>(); this.linkedL = new LinkedList<>(); } public int getTicket(int ticketId){ if (tickectInfoMap.containsKey(Integer.valueOf(ticketId))){ int info = tickectInfoMap.get(Integer.valueOf(ticketId)); linkedL.removeFirstOccurrence(ticketId); linkedL.addLast(ticketId); return info; } return -1; } public void putTicket(int ticketId, int ticketInfo){ if (this.capacity <= 0){ return; } if (tickectInfoMap.size() >= this.capacity){ Integer toRemove = linkedL.getFirst(); tickectInfoMap.remove(toRemove); linkedL.removeFirst(); } tickectInfoMap.put(Integer.valueOf(ticketId), Integer.valueOf(ticketInfo)); linkedL.addFirst(ticketId); } } }