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的更多相关文章

  1. [Swift]LeetCode146. LRU缓存机制 | LRU Cache

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. LeetCode146:LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. 【Leetcode146】LRU Cache

    问题描述: 设计一个LRU Cache . LRU cache 有两个操作函数. 1.get(key). 返回cache 中的key对应的 val 值: 2.set(key, value). 用伪代码 ...

  4. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  5. 【leetcode】LRU Cache

    题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...

  6. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  7. LRU Cache实现

    最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...

  8. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. [LintCode] LRU Cache 缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  10. LRU Cache [LeetCode]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. goroutine并发之callback回调

    玩玩 package main import ( "fmt" "strings" "sync" ) var wait sync.WaitGr ...

  2. Codeforces Round #594 (Div. 2)

    传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...

  3. Git 提交代码到远程仓库

    Git 命令 一.Git如何把本地代码推送到远程仓库 1. 拉取指定分支代码 git clone -b dev https://github.com/crazyfzw/RecycleViewWithH ...

  4. layUI学习第二日:非模块化方法使用layUI

    layUI采用非模块化方式(即所有模块一次性加载),操作示例代码如下(如果问怎么创建项目和工具,参考layUI学习第一日的步骤): 运行的结果如下: 运行的显示不会太持久,过几秒就会消失,具体封装的代 ...

  5. POJ1961Period(kmp+循环节)

    传送门 题目大意:输出字符串所有前缀的循环节个数,下标从1开始,i 和1-i循环节的个数 题解:网上摘得 KMP最小循环节.循环周期: 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为 ...

  6. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

    F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...

  7. vue表格合并行的一个实例

        一.element控件实现 在平常的应用中,需要用到合并单元格的操作,在Excel中,这种操作很好实现,但在实际项目中,常常需要借助element控件来实现. 下面是element中的一个实例 ...

  8. jquery延迟加载

    jquery实现图片延时加载,实现原理:不设置img的src地址,把地址存在img的alt中,当img标签出现在可视区域,alt值传给src.为避免看到替换文本alt,把字体的颜色设置为背景的颜色,如 ...

  9. 当接口请求体里的日期格式跟web页面日期格式不一致时,该如何处理呢?

    首先引入Unix纪元时间戳的概念:即格林威治时间(GMT,Greenwich Mean Time)1970年1月1日00:00:00,到当前时间的秒数.单位为秒(s). 那么当前时间的Unix纪元时间 ...

  10. Jenkins 插件 升级站点 镜像 好用的 2019年11月

    这两天开始学Jenkins 用docker下载了一个镜像运行 , 版本太老了.初始化插件各种报错:版本低/ 更新失败等. dockerhub里面的版本才 2.60 然后又从Jenkins.io 官网上 ...