LeetCode--146--medium--LRUCache

package com.app.main.LeetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 146
 *
 * medium
 *
 * 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.
 *
 * The cache is initialized with a positive capacity.
 *
 * Follow up:
 * Could you do both operations in O(1) time complexity?
 *
 * Example:
 *LRUCache cache = new LRUCache(2);
        *
        *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
 * Created with IDEA
 * author:Dingsheng Huang
 * Date:2020/2/14
 * Time:上午1:53
 */
class LRUCache {

    // double linked list node
    class Node {
        int k; // using when remove from map
        int v;
        Node pre;
        Node next;
    }

    // head node
    private Node head;

    // tail node
    private Node tail;

    // max size
    private int capacity;

    // current size count
    private int count;

    Map<Integer, Node> map = new HashMap<>();

    public LRUCache(int capacity) {
        // init..
        this.capacity = capacity;
        count = 0;
        head = new Node();
        tail = new Node();
        head.pre = tail;
        head.next = null;
        tail.next = head;
        tail.pre = null;
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        Node curr = map.get(key);
        // adjust double linked list
        deleteNode(curr);
        trans2Head(curr);
        return curr.v;
    }

    public void put(int key, int value) {
        if (map.containsKey(key)) {
            Node curr = map.get(key);
            curr.v = value;
            deleteNode(curr);
            trans2Head(curr);
        } else {
            if (count < capacity) {
                count++;
            } else {
                map.remove(tail.next.k);
                deleteNode(tail.next);
            }
            Node curr = new Node();
            curr.k = key;
            curr.v = value;
            trans2Head(curr);
            map.put(key, curr);
        }
    }

    private void deleteNode(Node node) {
        Node pre = node.pre;
        Node next = node.next;
        pre.next = next;
        next.pre = pre;
    }

    private void trans2Head(Node node) {
        Node pre = head.pre;
        pre.next = node;
        node.pre = pre;
        node.next = head;
        head.pre = node;
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
发布了187 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/huangdingsheng/article/details/104306086
今日推荐