首页 > 代码库 > [LeetCode]LRU Cache

[LeetCode]LRU Cache

题目:实现一个LRU Cache

算法:

  • 双向链表 + HashMap
  • get:若节点不存在,返回-1;否则返回节点value,并将节点调整到head
  • set(key, value):
    • 若key已经存在:更新value,并将节点调整到head;
    • 若key不存在:若cache容量足够,将节点压入链表头部;若cache容量不足,淘汰链表尾部节点,并将新节点压入链表头部


import java.util.HashMap;


public class LRUCache {
    /**
     * Structure
     * 
     * @author ouyangyewei
     */
    public class CacheNode {
        public int key;
        public int value;
        public CacheNode prev;
        public CacheNode next;
        
        public CacheNode() {
            // nothing
        }
        public CacheNode(int key, int value) {
            this.key = key;
            this.value = http://www.mamicode.com/value;>

[LeetCode]LRU Cache