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 ...
随机推荐
- Day4- Python基础4 深浅拷贝、三目运算、列表生成式,迭代器&生成器、装饰器
本节内容: 1.深浅拷贝 2.三目运算 3.迭代器和生成器 4.装饰器 1.深浅拷贝 拷贝意味着对数据重新复制一份,深浅拷贝的含义就是:对于修改复制的数据是否会影响到源数据,拷贝操作对于基本数据结构需 ...
- layUI学习第二日:非模块化方法使用layUI
layUI采用非模块化方式(即所有模块一次性加载),操作示例代码如下(如果问怎么创建项目和工具,参考layUI学习第一日的步骤): 运行的结果如下: 运行的显示不会太持久,过几秒就会消失,具体封装的代 ...
- 推荐|MathType的使用技巧
前言 持续更新中,敬请期待... 数学学科 制作新的数学符号 不包含于符号:输入$\not\subseteq,然后按回车键enter即可: 分式\(\cfrac{3-x}{2x-1}\)符号:输入$\ ...
- Note | 常用指令,工具,教程和经验笔记
目录 图像处理 机器学习和数学 编程环境和工具 写作工具 其他 图像处理 获取图像频域并分解为高低频:https://www.cnblogs.com/RyanXing/p/11630493.html ...
- angularjs中ng-class常用写法,三元表达式、评估表达式与对象写法
壹 ❀ 引 ng-class可以说在angularjs样式开发中使用频率特别高了,这不我想利用ng-class的三元运算符的写法来定义一个样式,结果怎么都想不起来正确写法,恼羞成怒还是整理一遍吧,那 ...
- 基于Node 的http转发demo,项目中请使用express+http-proxy-middleware
var http = require("http"); var data = ''; function getData() { const options = { host: 'w ...
- Docker的入门及常用命令
Docker入门及常用命令 1. 各个容器之间是相互隔离状态: 这样减少了我们软件之间的影响. 2. docker是os层虚拟化架构的一种产品体现, os层虚拟化架构出来的操作系统需要和宿主机操作系统 ...
- 多线程通信的两种方式? (可重入锁ReentrantLock和Object)
(一)Java中线程协作的最常见的两种方式: (1)利用Object的wait().notify()和notifyAll()方法及synchronized (2)使用Condition.Reentra ...
- 关于matlab2014a中生成dll文件,打包成com组件出现的问题和解决方法
问题1:matlab2014a破解不完整,容易导致package打包失败 解决方法:1.下载破解文档:链接: http://pan.baidu.com/s/1eRJ4E2I 密码: 44th 2.下载 ...
- WPF属性绑定实现双向变化
WPF依赖项属性可以实现属性的绑定,成功绑定之后只要修改后台绑定的属性,即可UI同步自动更新绑定的值,无需手动刷新界面:同样,前台的值变化后,通过获取绑定的属性值也可获取UI变化后的值,实现双向变化的 ...