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 ('ll 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 is reached its capacity, it should invalidate the least recently used item before inserting a new item.
Problem Solving Ideas:
To implement the LRU algorithm buffer class, because the cache will have frequent read and change operations, so to have the appropriate data structure to make set and get complexity is very small, preferably near O (1).
There are two main ways of thinking:
First, with splay realization, splay is a BST, while in the search and modification will let that node up to the root node, but the operation is O (log (n)) level, and there is a problem, is that the tree may become a chain (normal nodes are query frequency from top to bottom, so soon, Averaging is less than O (log (n))). Splay implementations are too complex to be given here.
Second, with double linked list and HASHMAP realization.
The purpose of the
list is to record the order in which nodes are used. This is the way in which LRU is normally used. The
HashMap implementation uses key to locate the node object in the list, adds a node to the list, and inserts a HASHMAP. The
Gets or modifies the value of the node as required. The
modifies the node's usage time, which is to pull the node in the list to the list header.
at the first step, if the number of nodes is greater than the available capacity, the last node of the List is deleted. The
Java implementation is as follows:
Package Oj.leetcode;import java.util.*;p ublic class LRUCache {private int capacity; Private Node head, tail; Private Hashmap<integer, node> Keynodemap; public LRUCache (int capacity) {this.capacity = capacity; Head = new Node (-1,-1); tail = new Node (0, 0); Head.next = tail; Tail.pre = head; This.keynodemap = new Hashmap<integer, node> (); } public int get (int key) {node node = keynodemap.get (key); if (node! = null) {Movetohead (node); return node.value; } return-1; } public void set (int key, int value) {node node = null; if (Keynodemap.containskey (key)) {node = Keynodemap.get (key); Node.value = value; } else {node = new node (key, value); if (keynodemap.size () = = capacity) {Keynodemap.remove (RemoveTail ()); } keynodemap.put (key, node); } movEtohead (node); } private void Movetohead (node node) {if (Node.pre! = NULL | | Node.next! = NULL) {Node.next.pre = n Ode.pre; Node.pre.next = Node.next; } node.next = Head.next; head.next.pre = node; Node.pre = head; Head.next = node; } private int RemoveTail () {int lastkey =-1; if (tail.pre! = head) {Node lastnode = Tail.pre; Lastkey = Lastnode.key; LastNode.pre.next = tail; Tail.pre = Lastnode.pre; Lastnode = null; } return Lastkey; } class node{int key; int value; Node Pre; Node Next; public Node (int k, int v) {key = k; Value = V; } }}
Java for Leetcode 146 LRU Cache ' hard '