查看: 3759| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

LRU Cache

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
貌似最直接的方法还是用自己实现的LinkedList来做,需要注意的是能不能根据Node找到map的key值 --- 为了达到这一目的,我决定在Node 中同时存储key / value .

  1. import java.util.*;

  2. class ListNode {
  3.     int key, val;
  4.     ListNode prev, next;
  5.     public ListNode(int k, int v) {
  6.         key = k;
  7.             val = v;
  8.     }
  9. }

  10. public class LRUCache {
  11.     static int size, maxSize;
  12.     ListNode head, tail;
  13.     HashMap<Integer, ListNode> map;
  14.     // initialize LRUCache in constructor
  15.     public LRUCache(int capacity) {
  16.         size = 0;
  17.         maxSize = capacity;
  18.         map = new HashMap<>();
  19.         head = new ListNode(0, 0);
  20.         tail = new ListNode(0, 0);
  21.         head.next = tail;
  22.         tail.prev = head;
  23.     }
  24.    
  25.     public int get(int key) {
  26.         if (map.containsKey(key)) {
  27.             ListNode node = map.get(key);
  28.             moveToFirst(node);
  29.             return node.val;
  30.         } else {
  31.             return -1;
  32.         }
  33.     }
  34.    
  35.     public void set(int key, int value) {
  36.         if (maxSize == 0) {
  37.             return;
  38.         }
  39.         if (map.containsKey(key)) {
  40.             ListNode node = map.get(key);
  41.             node.val = value;
  42.             moveToFirst(node);
  43.         } else {
  44.             // this key doesn't exsit in the map's keySet;
  45.             
  46.             // if the cache is full, exclude the last one node;
  47.             if (size == maxSize) {
  48.                     ListNode toBeDeleted = tail.prev;
  49.                 map.remove(toBeDeleted.key);
  50.                 tail.prev = tail.prev.prev;
  51.                 tail.prev.next = tail;
  52.                 size--;
  53.             }   
  54.             ListNode oldNext = head.next;
  55.             ListNode newNext = new ListNode(key, value);
  56.             head.next = newNext;
  57.             newNext.next = oldNext;
  58.             oldNext.prev = newNext;
  59.             newNext.prev = head;
  60.             map.put(key, newNext);
  61.             size++;
  62.         }


  63.     }
  64.     private void moveToFirst(ListNode node) {
  65.         // get the node out of list
  66.         ListNode p = node.prev;
  67.         ListNode n = node.next;
  68.         p.next = n;
  69.         n.prev = p;
  70.         // put node at the head of list;
  71.         head.next.prev = node;
  72.         node.prev = head;
  73.         node.next = head.next;
  74.         head.next = node;
  75.     }
  76.         public static void main(String[] args) {
  77.                 LRUCache cache = new LRUCache(1);
  78.                 cache.set(2, 1);
  79.                 System.out.println(cache.get(2));
  80.                 cache.set(3, 2);
  81.                 System.out.println(cache.get(2));
  82.                 System.out.println(cache.get(3));
  83.         }

  84. }
复制代码

上一篇:关于 PartitionArray
下一篇:leetcode 149 --- Max Points on a line
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表