146 LRU Cache
class LRUCache { class Node{ int key; int value; Node prev; Node next; public Node(int key, int value){ this.key = key; this.value = value; } } // initialize the data strcutures we need , global vars HashMap<Integer, Node> map; int capacity, count; // for dll Node head, tail; // it's like dummy node, makes life easier // constructor public LRUCache(int capacity) { this.capacity = capacity; count = 0; map = new HashMap<>(); // use a double linked list to maintain the freshness of the nodes (key, value) // every time, we call a node, if it still exists in the map, how to update the freshness of the node? remove the node from the dll, and add it to the head of the dll. // so the map is used for quick get // the double linked list is used to maintain which node is still fresh , // the freshest is at the very begining head = new Node(0, 0); tail = new Node(0, 0); // connect head and tail , double linked head.next = tail; tail.prev = head; head.prev = null; tail.next = null; } public int get(int key) { // if the map contains Key, return the value , else , return -1, // update the freshness if(map.containsKey(key)){ Node node = map.get(key); deleteNode(node); add(node); return node.value; }else{ return -1; } } public void put(int key, int value) { // if the key already exists in the map, replace the value in the map, remove the key value from // double linked list, and add it back to the head of the linked list // else, add key value to the map, and add the key value pair to the head of the double linked list // after that, we need to count the size, if within capacity, do nothing, else , remove the // the last node, which is also the oldest node from the map and the double linked list if(map.containsKey(key)){ Node node = map.get(key); node.value = value; deleteNode(node); add(node); }else{ Node node = new Node(key, value); map.put(key, node); add(node); count++; // check if the size exceeds the capacity if(count > capacity){ Node old = tail.prev; map.remove(old.key); deleteNode(old); count--; } } } // helper functions that make life easier // used in many places in this class private void add(Node node){ // always add to the front Node recent = head.next; head.next = node; node.next = recent; node.prev = head; recent.prev = node; } private void deleteNode(Node node){ // this is why we use double linked list // so that we can use the previous pointer Node prev = node.prev; Node next = node.next; prev.next = next; next.prev = prev; } }
这个题的 node class, constructor, global vars , 怎么写 该注意
几点该注意的:
use helper functions, add, and deleteNode, add : add node to the front, deleteNode: deleteNode any node
put(key, value) function: first check if the key exists, if not, put and check if the size if out of capacity
get(key) : return the value if exists in the map, and update freshness of the node by removing first and add in the double linked list
用 head, tail dummy node,
在 hashmap 里, <integer, node>
class Node{
int key;
int value;
Node prev;
Node next;
public Node(int key, int value){
this.key = key;
this.value = value;
// initialize the data strcutures we need , global vars
HashMap<Integer, Node> map;
int capacity, count;
// for dll
Node head, tail; // it's like dummy node, makes life easier
// constructor
public LRUCache(int capacity) {
this.capacity = capacity;
count = 0;
map = new HashMap<>();
// use a double linked list to maintain the freshness of the nodes (key, value)
// every time, we call a node, if it still exists in the map, how to update the freshness of the node? remove the node from the dll, and add it to the head of the dll.
// so the map is used for quick get
// the double linked list is used to maintain which node is still fresh ,
// the freshest is at the very begining
head = new Node(0, 0);
tail = new Node(0, 0);
// connect head and tail , double linked
head.next = tail;
tail.prev = head;
head.prev = null;
tail.next = null;
}
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(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.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1); cache.put(2, 2); cache.get(1); // returns 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
posted on 2018-08-10 15:18 猪猪🐷 阅读(173) 评论(0) 收藏 举报
浙公网安备 33010602011771号