1. 程式人生 > >[Leetcode-146] LRU Cache 最近最少使用頁面置換演算法

[Leetcode-146] LRU Cache 最近最少使用頁面置換演算法

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物件 cache
LRUCache 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 , 替換為3
cache
.get(2); // returns -1 (not found) 因為2被淘汰,返回-1 cache.put(4, 4); // evicts key 1 淘汰1 用4替換1 cache.get(1); // returns -1 (not found) 因為1被淘汰,返回-1 cache.get(3); // returns 3 讀取3 cache.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);
    }
}