当前位置:网站首页>2022.02.13 - NC003. Design LRU cache structure
2022.02.13 - NC003. Design LRU cache structure
2022-07-06 08:21:00 【A CAI continues to work hard】
List of articles
1. subject


2. Ideas
(1) HashMap+ queue
- utilize HashMap Store key value pair data , Using queues to control the replacement of data .
3. Code
import java.util.*;
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public int[] LRU(int[][] operators, int k) {
Map<Integer, Integer> map = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();
int[] res = new int[operators.length];
int index = 0;
for (int i = 0; i < operators.length; i++) {
switch (operators[i][0]) {
case 1:
map.put(operators[i][1], operators[i][2]);
queue.offer(operators[i][1]);
if (queue.size() > k) {
map.remove(queue.poll());
}
break;
case 2:
if (map.containsKey(operators[i][1])) {
res[index++] = map.get(operators[i][1]);
queue.remove(operators[i][1]);
queue.offer(operators[i][1]);
} else {
res[index++] = -1;
}
break;
}
}
return Arrays.copyOfRange(res, 0, index);
}
}
边栏推荐
- [2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
- Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
- Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
- Analysis of Top1 accuracy and top5 accuracy examples
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- [Yugong series] creation of 009 unity object of U3D full stack class in February 2022
- LDAP應用篇(4)Jenkins接入
- Golang DNS write casually
- 1204 character deletion operation (2)
猜你喜欢

2.10transfrom attribute

C language custom type: struct

在 uniapp 中使用阿里图标

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

C language - bit segment

Wireshark grabs packets to understand its word TCP segment

IOT -- interpreting the four tier architecture of the Internet of things

【刷题】牛客网面试必刷TOP101

Yyds dry goods inventory three JS source code interpretation eventdispatcher

Make learning pointer easier (3)
随机推荐
matplotlib. Widgets are easy to use
使用 TiUP 升级 TiDB
Restore backup data on S3 compatible storage with br
From monomer structure to microservice architecture, introduction to microservices
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
IP lab, the first weekly recheck
MFC 给列表控件发送左键单击、双击、以及右键单击消息
Upgrade tidb with tiup
[Yugong series] February 2022 U3D full stack class 010 prefabricated parts
LDAP应用篇(4)Jenkins接入
好用的TCP-UDP_debug工具下载和使用
[untitled]
Configuring OSPF load sharing for Huawei devices
C语言自定义类型:结构体
Golang DNS write casually
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
VMware 虚拟化集群
Yyds dry goods inventory three JS source code interpretation eventdispatcher
C语言 - 位段
图像融合--挑战、机遇与对策