当前位置:网站首页>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);
}
}
边栏推荐
- 远程存储访问授权
- leetcode刷题 (5.28) 哈希表
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- ESP系列引脚说明图汇总
- Uibehavior, a comprehensive exploration of ugui source code
- 让学指针变得更简单(三)
- [Yugong series] creation of 009 unity object of U3D full stack class in February 2022
- 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
- The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
- Understanding of law of large numbers and central limit theorem
猜你喜欢

wincc7.5下载安装教程(Win10系统)
![[research materials] 2021 Research Report on China's smart medical industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] 2021 Research Report on China's smart medical industry - Download attached

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
![[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]](/img/ae/4e616882f6d68acdf8e885843e68a3.jpg)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]

Configuring OSPF load sharing for Huawei devices

IoT -- 解读物联网四层架构

指针进阶---指针数组,数组指针

Ruffian Heng embedded bimonthly, issue 49

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

Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
随机推荐
Hcip day 16
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
升级 TiDB Operator
Go learning notes (3) basic types and statements (2)
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
matplotlib. Widgets are easy to use
Make learning pointer easier (3)
Hill sort c language
从表中名称映射关系修改视频名称
Migrate data from SQL files to tidb
Wincc7.5 download and installation tutorial (win10 system)
wincc7.5下载安装教程(Win10系统)
PHP - Common magic method (nanny level teaching)
从 CSV 文件迁移数据到 TiDB
Wireshark grabs packets to understand its word TCP segment
Easy to use tcp-udp_ Debug tool download and use
Analysis of Top1 accuracy and top5 accuracy examples
Upgrade tidb with tiup
A Closer Look at How Fine-tuning Changes BERT
Upgrade tidb operator