Problem LRU Cache

Problem Description:

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

 Solution:

 1     public static class DoubleLinkedListNode {
 2         int val;
 3         int key;
 4         DoubleLinkedListNode pre;
 5         DoubleLinkedListNode next;
 6         DoubleLinkedListNode(int key, int val) {this.key = key; this.val = val;pre = null; next = null;}
 7     }
 8     private int capacity;
 9
10     private int len;
11     private HashMap<Integer, DoubleLinkedListNode> map =
12                                 new HashMap<Integer, DoubleLinkedListNode>();
13     private DoubleLinkedListNode head;
14     private DoubleLinkedListNode end;
15     public LRUCache(int capacity) {
16         this.capacity = capacity;
17
18         len = 0;
19     }
20
21     public int get(int key) {
22
23         if (map.containsKey(key)) {
24             DoubleLinkedListNode latest = map.get(key);
25             removeNode(latest);
26             setHead(latest);
27             return latest.val;
28         } else {
29             return -1;
30         }
31
32     }
33     public void removeNode(DoubleLinkedListNode node) {
34         DoubleLinkedListNode cur = node;
35         DoubleLinkedListNode pre = cur.pre;
36         DoubleLinkedListNode post = cur.next;
37
38         if (pre != null) {
39             pre.next = post;
40         } else {
41             head = post;
42         }
43
44         if (post != null) {
45             post.pre = pre;
46         } else {
47             end = pre;
48         }
49     }
50     public void setHead(DoubleLinkedListNode node) {
51         node.next = head;
52         node.pre = null;
53         if (head != null) {
54             head.pre = node;
55         }
56
57         head = node;
58         if (end == null) {
59             end = node;
60         }
61     }
62     public void set(int key, int value) {
63
64         if (map.containsKey(key)) {
65             DoubleLinkedListNode oldNode = map.get(key);
66             oldNode.val = value;
67             removeNode(oldNode);
68             setHead(oldNode);
69         } else {
70             DoubleLinkedListNode newNode = new DoubleLinkedListNode(key, value);
71             if (len < capacity) {
72                 setHead(newNode);
73                 map.put(key, newNode);
74                 len++;
75             } else {
76                 map.remove(end.key);
77                 end = end.pre;
78                 if (end != null) {
79                     end.next = null;
80                 }
81
82                 setHead(newNode);
83                 map.put(key, newNode);
84             }
85         }
86
87     }

Problem LRU Cache

时间: 2024-08-31 03:39:32

Problem LRU Cache的相关文章

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

leetcode || 146、LRU Cache

problem: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise retur

LeetCode – LRU Cache (Java)

Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return

146. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i

LRU Cache的简单实现

Cache这个东西可以说无处不在,处理器中的TLB,Linux系统中的高速页缓存,还有很多人熟知的开源软件memcached,都是cache的一种实现.LRU是Least Recently Used的缩写,即最近最少使用,是常用cache算法中的一种.因为cache的存储空间相对于后端存储来说更有限,将cache空间和后端存储空间映射后,还需要一些算法来解决cache满的问题并保证效率,LRU就是在cache满了以后,将最近最少访问到的内容移除,然后将新的内容放入cache,新的内容也成为了最近

【LRU Cache】cpp

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

LRU Cache (9)

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

[leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高. 广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单.而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为了能在O(1) 时间获得key 需要类hash的结构,在python里,就用字典. 接下来的事情是,我