Title: Implementing an LRU Cache
Algorithm:
- Doubly linked list + HashMap
- Get: Returns 1 if the node does not exist, otherwise returns the node value. and adjust the node to head
- Set (key, value):
- If key already exists: Update value. and adjust the node to head;
- If key does not exist: if cache capacity is sufficient. Press the node into the list header, if the cache capacity is insufficient. Obsolete linked list tail node. and press the new node into the list header
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewv3zwlvdxlhbmc=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Import Java.util.hashmap;public class LRUCache {/** * Structure * * @author ouyangyewei * * Public Class Cachenode {public int key; public int value; Public Cachenode prev; Public Cachenode Next; Public Cachenode () {//no} public cachenode (int key, int value) {this.key = Ke Y This.value = value; This.prev = null; This.next = null; }} private int capacity; private int currentcapacity; Private Cachenode head; Private Cachenode tail; Private Hashmap<integer, cachenode> Hashnode; public LRUCache (int capacity) {this.capacity = capacity; this.currentcapacity = 0; This.head = null; This.tail = null; This.hashnode = new Hashmap<integer, cachenode> (); } public int get (int key) {Cachenode node = hashnode.get (key); if (null = = node) {Return-1; } else {movetohead (node); return node.value; }} public void set (int key, int value) {if (null = = Hashnode.get (key)) {/** Cachenode is no T exists! */Cachenode node = new Cachenode (key, value); if (Currentcapacity < capacity) {/** enough capacity for add a node */addtohead (node); ++currentcapacity; } else {/** over the maximum capacity */Removeleastusenode (); Addtohead (node); Currentcapacity = capacity; } hashnode.put (key, node); } else {/** Cachenode is already exists */cachenode node = hashnode.get (key); Node.value = value; Movetohead (node); Hashnode.put (key, node); }}/** * Move a node to list ' s head * @param node */public void MovetohEAD (Cachenode node) {if (null! = Node.prev) {/** node locate behind the list ' s head */Cach Enode front = Node.prev; Cachenode behind = Node.next; if (null = = behind) {/** node locate at the list ' s tail */front.next = behind; tail = front; } else {front.next = behind; Behind.prev = Front; } Node.prev = null; Node.next = head; Head.prev = node; Head = node; }}/** * Add a node to list ' s head. * @param node */public void Addtohead (Cachenode node) {if (null = = head) {/** list is empty */ Head = node; tail = node; } else {/** list is not empty */Node.next = head; Head.prev = node; Head = node; }}/** * Remove The least use node, it means Remove * The list ' s tail node. */public void Removeleastusenode () {if (null! = Tail.prev) {hashnode.remove (Tail.key); tail = Tail.prev; Tail.next = null; } else {/** list have only one node */Hashnode.remove (Tail.key); head = NULL; tail = null; }}/public static void main (string[] args) {//LRUCache LRUCache = new LRUCache (4);//LRUCACHE.S ET (1, 1);//lrucache.set (0, 0);//Lrucache.get (1);//Lrucache.get (0);//Lrucache.set (2, 2);// Lrucache.set (4, 4);//Lrucache.get (1);//Lrucache.get (0);//Lrucache.get (0);//LruCache . Set (8, 8);//Lrucache.set (7, 7);//Lrucache.set (5, 5);//Lrucache.set (4, 4);//Lrucache.set (3 , 3);//Lrucache.set (2, 2);//Lrucache.get (3);//Lrucache.get (4);//LruCache LruCache = new LR Ucache (1);//LRUCACHE.SET (2, 1);//System.out.println (Lrucache.get (2));//Lrucache.set (3, 2);//System.out.println (LruCache. Get (2));//System.out.println (Lrucache.get (3));//LruCache LruCache = new LruCache (2);//Lrucache.set (2, 1);//Lrucache.set (1, 2);//Lrucache.set (2, 3);//Lrucache.set (4, 1);//System.out.println (Lrucache.get (1));//System.out.println (Lrucache.get (2));/}}
[Leetcode] LRU Cache