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. <Math> 29 365

    29. Divide Two Integers class Solution { public int divide(int dividend, int divisor) { if(dividend ...

  2. golang+vscode开发环境的搭建

    一.windows下安装golang开发环境 (1)配置GOROOT变量,在系统变量中点击新建,变量值是golang安装文件夹目录 (2)配置Path变量,选中Path点编辑即可,在变量值后面追加;% ...

  3. 《细说PHP》 第四版 样章 第二章 PHP的应用与发展 5

    2.5  如何学习PHP PHP以其简单易学的特点,以及敏捷开发的优势,从一个几乎不被人知的开源项目,慢慢成长为技术人员首选的动态Web设计工具,与其他语言相比,PHP表现得更好.更快.更简单易学.尽 ...

  4. git分支合并解决冲突

    git分支合并,解决冲突 1.手动解决冲突 手动解决冲突,需要使用编辑器,把所有文件中出现的冲突地方修改,然后再添加到暂存区再提交 >>>>>>brancha so ...

  5. Java 实现 HtmlEmail 邮件发送功能

    引言 在平常的企业级应用开发过程中,可能会涉及到一些资讯通知需要传达,以及软件使用过程中有一些安全性的东西需要及早知道和了解,这时候在局域网之间就可以通过发送邮件的方式了.以下就是代码实现了: pac ...

  6. Docker学习——Dockerfile

    上一篇我们讲了docker的基本使用,掌握了前一篇,docker使用基本不成问题,但是要是你学习了Dockerfile,你会发现它使用起来有多方便了.项目最终部署时,我们希望docker容器打开时项目 ...

  7. 同步IDEA系列软件的设置,再也不用但心我的配置丢失了

    同步IDEA系列软件的设置 问题描述:重装idea,之前配置好的快捷键就没有了.之前一直是每隔几个月要把配置导出一下,上传百度云盘.现在好了,通过配置可以自动同步配置了.我再也不用但心配置丢失了. 快 ...

  8. Go template高级用法、深入详解、手册、指南、剖析

    入门示例 以下为test.html文件的内容,里面使用了一个template语法{{.}}. <!DOCTYPE html> <html> <head> <m ...

  9. 动软生成Model(dapper.common)

    <#@ template language="c#" HostSpecific="True" #><#@ output extension= ...

  10. java基础第十七篇之网络编程和装饰者模式

    1:网络概述 1.1 网络的发展Net 1964年,美国人---> 阿帕网--->以太网Internet 1.2 网络的通信协议 windows电脑,android手机,Mac平板---& ...