[Leetcode-146] LRU Cache 最近最少使用页面置换算法

来源:互联网 发布:阿沁淘宝店 编辑:程序博客网 时间:2024/06/06 00:56

  • 题目概要
  • AC 代码

0. 题目概要

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.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

//新建一个缓存容量为2的LRUCache对象 cacheLRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1);cache.put(2, 2);cache.get(1);       // returns 1    1被使用一次cache.put(3, 3);    // evicts key 2 此时2为最近最少使用的, 所以驱逐淘汰2 , 替换为3cache.get(2);       // returns -1 (not found)  因为2被淘汰,返回-1cache.put(4, 4);    // evicts key 1 淘汰1   用4替换1cache.get(1);       // returns -1 (not found)  因为1被淘汰,返回-1cache.get(3);       // returns 3 读取3cache.get(4);       // returns 4 读取4

AC 代码

/** * 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); */class LRUCache {    public LRUCache(int capacity) {    }    public int get(int key) {    }    public void put(int key, int value) {    }}

这里写图片描述

/** * 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); */import java.util.ArrayList;import java.util.HashMap;public class LRU {    public int cap;    public ArrayList<Integer> list = new ArrayList<Integer>();    public HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();    public LRU(int capacity){        this.cap = capacity;    }    public int get(int key){        if(map.containsKey(key)){            int index = list.indexOf(key);//确定key 在list的 index 下标            list.remove(index); // 原来链表中删除            list.add(0, key); //添加到链表中的对前面  【最近访问】            return map.get(key);//返回key 对应的value        }        return -1;//如若key不存在 返回-1    }    public void put(int key, int value){        if(map.containsKey(key)){            int index = list.indexOf(key);            list.remove(index);            list.add(0, key); // 移动到最近访问的位置            map.put(key, value);        }else{            list.add(0, key);            map.put(key, value);            if(list.size() > cap){                int rm = list.get(list.size()-1); //获得链表最后一个超出缓存大小的元素的  下标rm                list.remove(list.size()-1); //从链表中删除末尾元素  超出缓存大小的元素                map.remove(rm); // 从哈希表中 删除对应的 溢出队列的缓存元素            }        }    }}

下面贴出, 其他人提交的最快的代码:

import java.util.LinkedHashMap;import java.util.Map;public class LRUCache {    private LinkedHashMap<Integer, Integer> cache;    public LRUCache(int capacity) {        cache = new LinkedHashMap<Integer, Integer>(capacity, 1f, true) {            protected boolean removeEldestEntry(Map.Entry eldest) {                return size() > capacity;            }        };    }    public int get(int key) {        return cache.getOrDefault(key, -1);    }    public void put(int key, int value) {        cache.put(key, value);    }}
阅读全文
0 0