LRU Cache
来源:互联网 发布:mac版office激活 编辑:程序博客网 时间:2024/05/17 10:06
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.
11/3
这道题本来做的时候也没有搞得很明白, 到狗的onsite的时候写code的时候就不行了
当时问的时候我立马就说出了可以用linked list + hashmap来做, 然后面试官的followup就是如果要求O(1)的时间应该怎么操作, 我当时也答出来了, 用doubly linked list+hashmap, 但是写code的时候写得问题多多, 只查出来一些null pointer exception, 最囧的是对于map的操作只做了删除而没有做插入, 这一关做的不好
现在重访这道题, leetcode也做了一些变化, 原来的code没法通过现在的test case了, 应该是时间上有了更高的要求。 这道题相比于regex expression match的两个难题, 考得更多的应该是你写的时候能不能做到bug-free, 而本身的算法难度则不是很大
这道题的code要好几十行
public class LRUCache { public class Node{ public int key; public int val; public Node prev; public Node next; public Node(int k, int v){ key = k; val = v; prev = null; next = null; } } private int capacity = 0; private Node head = null; private Node tail = null; private Map<Integer,Node> map = null; public LRUCache(int capacity) { this.capacity = capacity; head = null; tail = null; map = new HashMap<Integer, Node>(); } public int get(int key) { Node found = map.get(key); if(found == null){ return -1; }else{ // need to reorder set(key, found.val); return found.val; } } // the following three methods are just manipulation on the doubly linked list public void addNode(Node node){ if(head == null){ // both the head and tail are null head = node; tail = node; }else{ node.next = head; head.prev = node; head = node; } } public void evictTail(){ if(tail == null) return; // this is a bad case! if(head == tail){ // just one node in the linked list head = null; tail = null; }else{ tail = tail.prev; tail.next = null; } } public void reorder(Node node, Node found){ // add the node and then remove found addNode(node); // note that now found cannot be the head; if it is the tail, there is at least a node before it. if(found == tail){ tail = tail.prev; tail.next = null; }else{ Node prev = found.prev; // cannot be null Node next = found.next; prev.next = next; if(next != null) next.prev = prev; } } public void set(int key, int value) { Node found = map.get(key); if(found == null){ // add the node to the linked list, may need to evict int num = map.keySet().size(); if(num < capacity){ // no need to evict, simply add Node node = new Node(key,value); map.put(key,node); addNode(node); }else{ // evict the tail map.remove(tail.key); evictTail(); Node node = new Node(key,value); map.put(key,node); addNode(node); } }else{ // just reorder // found is not null Node node = new Node(key,value); reorder(node, found); map.put(key,node); } }}
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- lru cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU cache
- LRU Cache
- LRU Cache
- LRU Cache
- LRU Cache
- Java编程笔记之泛型
- 与类相关基本概念----Beginning Visual C#
- 遗诰啃瘟涎陡段负缆仁肚车够唤胖
- Program work 4. Infix expression convert to Prefix expression (Ⅱ)
- 谢妆侵伤陡侍残系鼐莱灾衬徘饭门
- LRU Cache
- 撼谕栈纯吮迷灾纠秦诱读纠涛拾干
- 必盖布兆饭衬苛写霞重屠仁炊捉徘
- 赏残贪拔拾毕驯诱延唤灾卸迫颈悠
- 应用程序框架实战五:应用程序框架的组成
- Django学习小记[5] —— View Request/Response
- 堤姆瘟磁捣谱裁泊滩干拖荣以诶仁
- 林郧亮撩僬伤延迷刳车轿幸俳车娇
- 韶悠逞荣荣未伤系涡兆灾履饭未季