• leetcode--LRU Cache


    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    set(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. 

    Have you been asked this question in an interview? 

    The main point of this problem is to guarantee each operation cost O(1) time, This is the main requirement of cache.

    Since we need to remove a record from the cache and keep all records in order,  this invloves searching, deletion and insertion. The best data structure to do this is the

    one which is a combination of HashMap and doubly-linked list. So, I use a HashMap<Integer, Node>, where Node is class defined by myself.

    public class LRUCache {
       public LRUCache(int capacity) {
            lruMap = new HashMap<Integer, Node>();
    		this.capacity = capacity;
    		sz = 0;		
        }
        
        public int get(int key) {
            int result = -1;
    		if(lruMap.containsKey(key)){
    			moveToHead(lruMap.get(key));
    			lruMap.put(key, first);
    			result = first.value;
    		}
    		return result;
        }
        
        public void set(int key, int value) {
            if(lruMap.containsKey(key)){
    			Node temp = lruMap.get(key);
    			temp.value = (temp.value == value )? temp.value : value;
    			moveToHead(temp);
    			lruMap.put(key, first);
    		}
    		else if(sz < capacity){
    			Node node = new Node();
    			node.key = key;
    			node.value = value;
    			if(sz == 0){
    				first = node;
    				last = node;
    			}
    			else{
    				first.prev = node;
    				node.next = first;
    				first = node;
    			}
    			lruMap.put(key, node);
    			++sz;
    		} 
    		else{
    			Node node = new Node();
    			node.key = key;
    			node.value = value;
    			int lastKey = last.key;
    			if(sz == 1){
    				last = null;
    				first = null;
    				first = node;
    				last = node;
    				
    			}
    			else{
    				last = last.prev;
    				last.next = null;
    				first.prev = node;
    				node.next = first;
    				first = node;				
    			}
    			lruMap.remove(lastKey);
    			lruMap.put(key, node);			
    		}
        }
    	
    	public  void moveToHead(Node node){
    		if( node.prev == null)   //the node is already the head. Do nothing
    			return;
    		else{
    			node.prev.next = node.next;
    			if(node.next != null)
    				node.next.prev = node.prev;
    			else
    				last = node.prev;
    			first.prev = node;
    			node.prev = null;
    			node.next = first;
    			first = node;			 
    		}
    	}
    	
    	private HashMap<Integer, Node> lruMap;
    	private int capacity;
    	private Node first;
    	private Node last;
    	private int sz;
    }
    
    class Node{
    	int key; //we need use it to remove a node in the HashMap;
    	int value;
    	Node prev;
    	Node next;
    	//using the default constructor is enough
    }
    

      

  • 相关阅读:
    EOS之session的数据获取
    c# 数据库操作之ACCESS
    基础之创建与导出
    dotNET5的MVC页面传值方式总结
    dotNET开发之MVC中Controller返回值类型ActionResult方法总结
    C# 计算农历日期方法(2021版)
    普通邮箱设置客户端授权码并开启stmp服务以及关于QQ邮箱“命令顺序不正确。 服务器响应为:Error: need EHLO and AUTH first !”问题全指导
    13 张图,深入理解 Synchronized
    Springboot 注解大全
    python中的print()函数的学习-1
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3577651.html
Copyright © 2020-2023  润新知