@@ -0,0 +1,1651 @@
+package problems;%0A%0Aimport java.util.HashMap;%0A%0Apublic class LRUCache %7B%0A%0A    private int cap;%0A    private Node head, tail;%0A    private HashMap%3CInteger, Node%3E map;%0A%0A    private class Node %7B%0A        int key, val;%0A        Node pre, next;%0A%0A        public Node(int k, int v) %7B%0A            this.key = k;%0A            this.val = v;%0A            pre = next = null;%0A        %7D%0A    %7D%0A%0A    public LRUCache(int capacity) %7B%0A        this.cap = capacity;%0A        this.head = this.tail = null;%0A        this.map = new HashMap%3CInteger, Node%3E();%0A    %7D%0A%0A    public int get(int key) %7B%0A        if (!map.containsKey(key))%0A            return -1;%0A        Node t = map.get(key);%0A        moveBack(t);%0A        return t.val;%0A    %7D%0A%0A    public void set(int key, int value) %7B%0A        if (map.containsKey(key)) %7B%0A            Node t = map.get(key);%0A            t.val = value;%0A            moveBack(t);%0A        %7D else %7B%0A            if (map.size() == cap) %7B%0A                map.remove(head.key);%0A                remove(head);%0A            %7D%0A            Node t = new Node(key, value);%0A            add(t);%0A            map.put(key, t);%0A        %7D%0A    %7D%0A%0A    private void remove(Node t) %7B%0A        if (t.pre != null)%0A            t.pre.next = t.next;%0A        else%0A            head = t.next;%0A        if (t.next != null)%0A            t.next.pre = t.pre;%0A        else%0A            tail = t.pre;%0A        t.pre = t.next = null;%0A    %7D%0A%0A    private void add(Node t) %7B%0A        if (head == null)%0A            head = tail = t;%0A        else %7B%0A            tail.next = t;%0A            t.pre = tail;%0A            tail = t;%0A        %7D%0A    %7D%0A%0A    private void moveBack(Node t) %7B%0A        remove(t);%0A        add(t);%0A    %7D%0A%7D%0A

Add LRU Cache