@@ -0,0 +1,1926 @@
+public class LRUCache %7B%0A%0A    int capacity;%0A    Map%3CInteger, Item%3E map = new HashMap%3CInteger, Item%3E();%0A    Item head = null, tail = null;%0A    %0A    class Item %7B%0A        public int key;%0A        public int value;%0A        public Item prev;%0A        public Item next;%0A%0A        public Item(int key, int value) %7B%0A            this.key = key;%0A            this.value = value;%0A        %7D%0A    %7D%0A    %0A    public LRUCache(int capacity) %7B%0A        this.capacity = capacity;%0A        %0A    %7D%0A    %0A    public int get(int key) %7B%0A        if (map.containsKey(key)) %7B%0A            Item item = map.get(key);%0A            remove(item);%0A            setHead(item);%0A            return item.value;%0A        %7D%0A        return -1;%0A    %7D%0A    %0A    public void put(int key, int value) %7B%0A        if (map.containsKey(key)) %7B%0A            Item item = map.get(key);%0A            item.value = value;%0A            remove(item);%0A            setHead(item);%0A        %7D else %7B%0A            Item item = new Item(key, value);%0A            if (map.size() %3E= capacity) %7B%0A                map.remove(tail.key);%0A                remove(tail);%0A                setHead(item);%0A            %7D else %7B%0A                setHead(item);%0A            %7D%0A            map.put(key, item);%0A        %7D  %0A    %7D%0A    private void remove(Item item) %7B%0A        if (item.prev != null) %7B%0A            item.prev.next = item.next;%0A        %7D else %7B%0A            head = item.next;%0A        %7D%0A        if (item.next != null) %7B%0A            item.next.prev = item.prev;%0A        %7D else %7B%0A            tail = item.prev;%0A        %7D%0A    %7D%0A%0A    private void setHead(Item item) %7B%0A        item.next = head;%0A        item.prev = null;%0A%0A        if (head != null)%0A            head.prev = item;%0A        head = item;%0A%0A        if (tail == null)%0A            tail = head;%0A    %7D%0A%7D%0A%0A/**%0A * Your LRUCache object will be instantiated and called as such:%0A * LRUCache obj = new LRUCache(capacity);%0A * int param_1 = obj.get(key);%0A * obj.put(key,value);%0A */%0A

146. LRU Cache