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. 8. java 面向对象

    一.面向对象特征 1. 封装 方法就是一种封装 关键字private也是一种封装 封装就是讲一些逻辑细节信息隐藏起来,对于外界不可见:外界只需调用我即可: 一旦使用了private进行修饰,那么本类当 ...

  2. Day7 - Python基础7 面向对象

    本节内容: 1:概述 2:类.对象和方法的创建 3:面向对象三大特性,封装.继承和多态. 4:面向对象中高级篇:类成员:字段.方法.属性 5:类成员的修饰符 6:类的特殊成员 1.概述 面向过程:根据 ...

  3. pytroch中cp27m和cp27mu的区别及判别方法

    python中m和mu的区别 从https://download.pytorch.org/whl中下载pytorch安装包时常会发现有相同版本的安装包有cp27m和cp27mu两种,其中cp27m是u ...

  4. 剑指Offer-31.整数中1出现的次数(从1到n整数中1出现的次数)(C++/Java)

    题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...

  5. prerender-spa-plugin预渲染踩坑

    为什么要使用预渲染? 为了应付SEO(国内特别是百度)考虑在网站(vue技术栈系列)做一些优化.大概有几种方案可以考虑: 服务端做优化: 第一,ssr,vue官方文档给出的服务器渲染方案,这是一套完整 ...

  6. javascript中优雅的处理async和await异常

    let handler = async function(needErr) { return new Promise((resolve, reject) => { if (needErr) { ...

  7. Mybatis关联查询之一

    MyBatis的关联查询之一对多,多对一 在关系型数据库中,我们经常要处理一对多,多对一和多对多的关系. 一对多,多对一 一.entity 实体类 public class SmbmsRole { p ...

  8. 拎壶学python3-----(2)python之if语句用法

    在生活中我们经常遇到各种选择,比如玩色子,猜大小,再比如选择未来另一半.python也经常会遇到这样的选择,这时候if语句显得尤为重要. 下边我们看一个简单的例子 如果是二选一怎么做呢?如下 如果多个 ...

  9. C语言--计算程序执行时间

    C语言–计算程序执行时间1. gettimeofday精度1us #include<stdio.h>#include<sys/time.h> int main(){ /* 定义 ...

  10. Django JsonResponse 不自动设置 cookie 的解决方案

    [背景] 目前在做一个前后端分离的 web 项目,后端使用的是 django 框架,所有的 API 都只返回 json :就在这个过程中遇到了一个问题,那就是对于所有的 JsonResponse dj ...