Leetcode146-lru-cache
Leetcode146-lru-cache
int capacity;
int size;
Map<Integer, ListNode> map = new HashMap<Integer, ListNode>();
ListNode head;
ListNode last; class ListNode {
int key;
int value;
ListNode prev;
ListNode next; public ListNode(int key, int val) {
this.key = key;
this.value = val;
prev = null;
next = null;
} public int getKey() {
return key;
} public void setKey(int key) {
this.key = key;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
}
} public LRUCache(int capacity) {
this.capacity = capacity;
} public int get(int key) { if (map.containsKey(key)) {
return key;
}
return -1;
} public void put(int key, int value) { if (map.containsKey(key)) { moveNode(key, value); } else { if (size < capacity) {
insertNode(key, value);
size++;
} else {
insertNode(key, value);
last = last.next;
}
}
} public void insertNode(int key, int value) {
ListNode node;
node = new ListNode(key, value);
node.next = head;
head = node;
ListNode current = last;
while (current.next != null) {
current = current.next;
}
current.next = node;
} public void moveNode(int key, int value) {
ListNode node;
node = new ListNode(key, value);
node.next = head;
head = node;
while (node.next.getKey() != key && node.next.getKey() != last.getKey()) {
node = node.next;
}
node.next = node.next.next;
ListNode current = last;
while (current.next.getKey() != key && current.next.getKey() != last.getKey()) {
current = current.next;
}
current.next = current.next.next;
}
Leetcode146-lru-cache的更多相关文章
- [Swift]LeetCode146. LRU缓存机制 | LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LeetCode146:LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【Leetcode146】LRU Cache
问题描述: 设计一个LRU Cache . LRU cache 有两个操作函数. 1.get(key). 返回cache 中的key对应的 val 值: 2.set(key, value). 用伪代码 ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LRU Cache实现
最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LintCode] LRU Cache 缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LRU Cache [LeetCode]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- css3的新属性 新增的颜色--- 透明度---两种渐变---定义多张背景图--background-size
css31==>颜色的6种表示的方法有6种表示颜色的方法 关键字 rgb rgba(css3) 16进制 hsl hsla hsla h=>是色相,值为360, s=>饱和度,0%- ...
- Java 的synchronized关键字使用
synchronized 关键字是实现锁的一种方式,是在jvm层面实现的非公平锁,以下是使用synchronized的四种方式 synchronized 特性: 1.非公平锁 2.可重入性 1.作用在 ...
- luoguP4022 [CTSC2012]熟悉的文章
题意 显然这个\(L\)是可以二分的,我们只需要判断\(L\)是否合法即可. 显然有一个\(O(n^2)\)的DP: 设\(f_i\)表示当前匹配到\(i\)的最大匹配长度. \(f_i=max(f_ ...
- docker /var/lib/docker/aufs/mnt 目录满了怎么清理
1.创建脚本文件 vi cleandocker.sh 内容如下: #!/bin/sh echo "==================== start clean docker contai ...
- IDEA debug工具使用
参考:https://www.cnblogs.com/jajian/p/9410844.html
- __module__和__class__
目录 一.__module__ 二.__class__ # lib/aa.py class C: def __init__(self): self.name = 'SB' # index.py fro ...
- CentOS7下rsync服务端与Windows下cwRsync客户端实现数据同步配置方法
最近需求想定期备份服务器d盘的数据到Linux服务器上面,做个笔记顺便写下遇到的问题 以前整过一个win下的cwrsync(客户端)+rsync(服务端:存储)的bat脚本 和整过一个Linux下的r ...
- MySQL UNSIGNED和ZEROFILL属性
UNSIGNED 这个属性就是标记数字类型是无符号的,和C/C++语言中的unsigned含义是一样的,int signed的类型范围是-2147483648~2147483648,而int unsi ...
- LINUX CFS 调度tick逻辑,即check_preemt_tick解析
计算当前task在这个tick周期实际用时delta_exetime, 更新当前task的vruntime; 根据权重,重新计算调度period,计算当前task的应得时间片slice(idle_ru ...
- MySQL入门——在Linux下安装和卸载MySQL
MySQL入门——在Linux下安装和卸载MySQL 摘要:本文主要学习了如何在Linux系统中安装和卸载MySQL数据库. 查看有没有安装过MySQL 使用命令查看有没有安装过: [root@loc ...