当前位置:网站首页>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);
}
}
边栏推荐
- Migrate data from CSV files to tidb
- Migrate data from a tidb cluster to another tidb cluster
- [secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
- "Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
- Permutation and combination function
- Migrate data from SQL files to tidb
- VMware 虚拟化集群
- 【MySQL】数据库的存储过程与存储函数通关教程(完整版)
- Configuring OSPF load sharing for Huawei devices
- 使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
猜你喜欢
From monomer structure to microservice architecture, introduction to microservices
C语言 - 位段
Understanding of law of large numbers and central limit theorem
Make learning pointer easier (3)
指针和数组笔试题解析
【MySQL】日志
【云原生】手把手教你搭建ferry开源工单系统
Online yaml to CSV tool
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
随机推荐
Migrate data from a tidb cluster to another tidb cluster
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
C语言自定义类型:结构体
leetcode刷题 (5.29) 哈希表
【MySQL】日志
It's hard to find a job when the industry is in recession
Use Alibaba icon in uniapp
[research materials] 2021 China online high growth white paper - Download attached
【云原生】手把手教你搭建ferry开源工单系统
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
Résumé des diagrammes de description des broches de la série ESP
[2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
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
Huawei cloud OBS file upload and download tool class
Easy to use tcp-udp_ Debug tool download and use
Golang DNS 随便写写
Online yaml to CSV tool
Introduction to backup and recovery Cr
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
matplotlib. Widgets are easy to use