LRU缓存(Leetcode146)

例题:
分析:

题目要求函数get和put要达到O(1)的时间复杂度,可以用 hashMap 来实现,因为要满足逐出最久未使用的元素的一个效果,还需要配合一个双向链表来共同实现。链表中的节点为一组key-value。

我们可以用双向链表来储存数据(key-value),当调用put方法添加数据时,可以将数据(key-value)添加到双向链表的队头,队头的元素表示最新使用的元素,越靠近队尾,就是最久未用的元素。

当调用get方法时,若存在此元素,则从双向链表中把该组数据(key-value)提到队头来。

代码实现:
java 复制代码
package leetcode;

import java.util.HashMap;

public class LRUCacheLeetcode146 {

    static class LRUCache {

        static class Node{
            Node next;
            Node prev;
            int key;
            int value;
            public Node(){

            }

            public Node(int key, int value) {
                this.key = key;
                this.value = value;
            }
        }

        static class DoublyLinkedList{
            Node head;
            Node tail;

            public DoublyLinkedList() {
                head = tail = new Node();
                head.next = tail;
                tail.prev = head;
            }

            //头部添加 head<->1<->2<->tail    假如添加3
            public void addFirst(Node newNode){
                Node oldFirst = head.next;
                oldFirst.prev = newNode;
                head.next = newNode;
                newNode.prev = head;
                newNode.next = oldFirst;
            }

            //已知节点删除  head<->1<->2<->tail  假如删除2
            public void remove(Node node){
                Node prev = node.prev;
                Node next = node.next;
                prev.next = next;
                next.prev = prev;
            }

            //尾部删除
            public Node removeLast(){
                Node last = tail.prev;
                remove(last);
                return last;
            }
        }

        private final HashMap<Integer, Node> map = new HashMap<>();
        private final DoublyLinkedList list = new DoublyLinkedList();
        private final int capacity;
        public LRUCache(int capacity) {
            this.capacity = capacity;
        }

        public int get(int key) {
            if(!map.containsKey(key)){
                return -1;
            }
            Node node = map.get(key);
            //hash表中存在该数据,改组数据应放到队头
            //先从中删除原始数据
            list.remove(node);
            //再将改组数据添加到队头
            list.addFirst(node);
            return node.value;
        }

        public void put(int key, int value) {
            if(map.containsKey(key)){ //更新
                Node node = map.get(key);
                node.value = value;
                list.remove(node);
                list.addFirst(node);
            }else{ //添加
                Node newNode = new Node(key, value);
                map.put(key, newNode);
                list.addFirst(newNode);
                if(map.size() > capacity){
                    Node removed = list.removeLast();
                    //删除hash表中的数据
                    map.remove(removed.key);
                }
            }
        }
    }

    public static void main(String[] args) {
        LRUCache cache = new LRUCache(2);
        cache.put(1, 1);
        cache.put(2, 2);
        System.out.println(cache.get(1)); // 1
        cache.put(3, 3);
        System.out.println(cache.get(2)); // -1
        cache.put(4, 4);
        System.out.println(cache.get(1)); // -1
        System.out.println(cache.get(3)); // 3
    }
}
相关推荐
xlq223229 小时前
22.多态(上)
开发语言·c++·算法
666HZ6669 小时前
C语言——高精度加法
c语言·开发语言·算法
sweet丶10 小时前
iOS MMKV原理整理总结:比UserDefaults快100倍的存储方案是如何炼成的?
算法·架构
遇见火星10 小时前
CentOS7 通过源码安装 Redis
数据库·redis·缓存
K哥112510 小时前
【9天Redis系列】基础+全局命令
数据库·redis·缓存
f***R810 小时前
redis分页查询
数据库·redis·缓存
云里雾里!10 小时前
力扣 209. 长度最小的子数组:滑动窗口解法完整解析
数据结构·算法·leetcode
CoderYanger11 小时前
递归、搜索与回溯-穷举vs暴搜vs深搜vs回溯vs剪枝:12.全排列
java·算法·leetcode·机器学习·深度优先·剪枝·1024程序员节
憨憨崽&12 小时前
进击大厂:程序员必须修炼的算法“内功”与思维体系
开发语言·数据结构·算法·链表·贪心算法·线性回归·动态规划
2***c43512 小时前
Redis五种用途
数据库·redis·缓存