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
}

  

转载于:https://www.cnblogs.com/averillzheng/p/3577651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值