当前位置:网站首页>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);
}
}
边栏推荐
- 【刷题】牛客网面试必刷TOP101
- Introduction to backup and recovery Cr
- [research materials] 2021 Research Report on China's smart medical industry - Download attached
- vulnhub hackme: 1
- Online yaml to CSV tool
- Analysis of Top1 accuracy and top5 accuracy examples
- IoT -- 解读物联网四层架构
- Uibehavior, a comprehensive exploration of ugui source code
- What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
- Upgrade tidb operator
猜你喜欢
Golang DNS 随便写写
ESP系列引脚說明圖匯總
Learn Arduino with examples
C language - bit segment
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
NFT smart contract release, blind box, public offering technology practice -- contract
[research materials] 2022 China yuancosmos white paper - Download attached
Easy to use tcp-udp_ Debug tool download and use
【MySQL】日志
Wireshark grabs packets to understand its word TCP segment
随机推荐
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
Leetcode skimming (5.29) hash table
Huawei cloud OBS file upload and download tool class
使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
leetcode刷题 (5.29) 哈希表
Understanding of law of large numbers and central limit theorem
PHP - Common magic method (nanny level teaching)
"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
On why we should program for all
LDAP應用篇(4)Jenkins接入
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
[t31zl intelligent video application processor data]
根据csv文件某一列字符串中某个数字排序
CAD ARX gets the current viewport settings
Analysis of pointer and array written test questions
JS select all and tab bar switching, simple comments
Oracle time display adjustment
ESP系列引脚说明图汇总
C language custom type: struct