leetcode刷题日志-146LRU缓存

在这里插入图片描述
思路:使用hashmap储存key,vaule,使用双向链表以快速查到尾结点(待逐出的节点),链表的题一定要在纸上画一下,不然连着连着就不知道连在哪里去了

class LRUCache {
   
    public class ListNode {
   
      int key;
      int value;
      ListNode next;
      ListNode pre;
      ListNode() {
   }
      ListNode(int key , int value) {
    this.key = key; this.value = value;}
      ListNode(int kye, int value, ListNode next) {
    this.key = key;this.value = value;this.next = next;}
      ListNode(int kye, int value, ListNode next,ListNode pre) {
    this.key = key;this.value = value;this.next = next;this.pre = pre;}
  }
    ListNode head;//头节点
    ListNode tail;//指向尾结点的前一个节点,方便逐出最久未使用关键字
    int capacity; //储存容量
    int cur_capacity;//储存当前容量
    Map<Integer,ListNode> map;//储存节点
    
    public LRUCache(int capacity) {
   
        this.head = new ListNode(0,0,null,null);
        this.capacity = capacity;
        this.cur_capacity = 0;
        this.map = new HashMap<>();
        this.tail = new ListNode(0,0,null,head);
        this.head.next = tail;
    }
    
    public int get(int key) {
   
        if(map.containsKey(key))  //最近使用到的key,将位置提前
        {
   
            if(map.get(key).next != null)
            {
   
                map.get(key).next.pre = map.get(key).pre;
                map.get(key).pre.next = map.get(key).next;
            }
            map.get(key).next = head.next;
            map.get(key).next.pre = map.get(key);
            map.get(key).pre = head;
            head.next = map.get(key);
            return map.get(key).value;
        }
        else
            return -1;
    }
    
    public void put(int key, int value) {
   
        if(map.containsKey(key)) //存在,将位置提前
        {
   
            map.get(key).value = value;
            if(map.get(key).next != null)
            {
   
                map.get(key).next.pre = map.get(key).pre;
                map.get(key).pre.next = map.get(key).next;
            }
            map.get(key).next = head.next;
            map.get(key).next.pre = map.get(key);
            map.get(key).pre = head;
            head.next = map.get(key);
        }
        else{
   //不存在
            if(this.cur_capacity < this.capacity) //容量足够,直接添加到头
            {
   
                ListNode temp = new ListNode();
                temp.key = key;
                temp.value = value;
                temp.next = head.next;
                if(temp.next != null)
                temp.next.pre = temp;
                temp.pre = head;
                head.next = temp;
                cur_capacity++;
                map.put(key,temp);
            }
            else//容量不够,移出尾结点,添加新节点到头
            {
   

                ListNode tail_pre = tail.pre;
                map.remove(tail_pre.key);
                tail.pre.pre.next = tail;
                tail.pre.next = null;
                tail.pre = tail.pre.pre;
                tail_pre.pre = null;
                ListNode temp = new ListNode();
                temp.key = key;
                temp.value = value;
                temp.next = head.next;
                temp.next.pre = temp;
                temp.pre = head;
                head.next = temp;
                map.put(key,temp);
            }
        }
    }
}

/**
 * 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);
 */

相关推荐

  1. LeetCode——146. LRU 缓存(HOT100)

    2024-01-29 05:44:05       24 阅读
  2. leetcodeHOT146. LRU 缓存

    2024-01-29 05:44:05       17 阅读
  3. LeetCode-热100:146. LRU 缓存

    2024-01-29 05:44:05       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 05:44:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 05:44:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 05:44:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 05:44:05       20 阅读

热门阅读

  1. Linux常用指令的整合

    2024-01-29 05:44:05       30 阅读
  2. 【物联网之·协议·ZigBee】

    2024-01-29 05:44:05       32 阅读
  3. 一维数组的学习

    2024-01-29 05:44:05       33 阅读
  4. bridge

    2024-01-29 05:44:05       32 阅读
  5. sql总结(高阶用法)

    2024-01-29 05:44:05       32 阅读
  6. paddle 动态图命名重复问题

    2024-01-29 05:44:05       35 阅读
  7. 707.设计链表(力扣LeetCode)

    2024-01-29 05:44:05       31 阅读