当前位置:网站首页>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);
}
}
边栏推荐
- Restore backup data on S3 compatible storage with br
- What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
- VMware 虚拟化集群
- Leetcode question brushing record | 203_ Remove linked list elements
- Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
- [cloud native] teach you how to build ferry open source work order system
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- [secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
- 使用 TiUP 升级 TiDB
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
猜你喜欢
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
将 NFT 设置为 ENS 个人资料头像的分步指南
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
Fibonacci sequence
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
C语言自定义类型:结构体
wincc7.5下载安装教程(Win10系统)
Ruffian Heng embedded bimonthly, issue 49
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
C语言 - 位段
随机推荐
Ruffian Heng embedded bimonthly, issue 49
A Closer Look at How Fine-tuning Changes BERT
图像融合--挑战、机遇与对策
CISP-PTE实操练习讲解
Upgrade tidb with tiup
hcip--mpls
Analysis of Top1 accuracy and top5 accuracy examples
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
Online yaml to CSV tool
Configuring OSPF load sharing for Huawei devices
3. File operation 3-with
Restore backup data on S3 compatible storage with br
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
将 NFT 设置为 ENS 个人资料头像的分步指南
Step by step guide to setting NFT as an ens profile Avatar
matplotlib. Widgets are easy to use
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
Leetcode question brushing (5.31) string
Leetcode question brushing (5.28) hash table
C language custom type: struct