Leetcode- LRUCache

关键是搞懂题目(不知道LRUCache的只能自己google了)。

然后用双向链表来模拟cache被get和set。但是naive implementation会exceed time limit。所以最大的关键是用一个HashMap来记录key在链表中的位置,这样子每次查询是O(1)的时间,否则O(n)。

这个也是很经典的用Map来加速双向链表查询的思路(前提是key要唯一)。

import java.util.*;

public class LRUCache {

	Map<Integer, DoubleLinkedListNode> hmap = new HashMap<Integer, DoubleLinkedListNode>();
	DoubleLinkedListNode head = null;
	DoubleLinkedListNode tail = null;
	int capa;

	public LRUCache(int capacity) {
		this.capa = capacity;
	}

	public int get(int key) {

		if (hmap.containsKey(key)) {
			DoubleLinkedListNode tnode = hmap.get(key);
			tnode = removeNode(tnode);
			pushFront(tnode);
			return tnode.value;
		}
		return -1;
	}

	public void set(int key, int value) {

		if (hmap.containsKey(key) == true) {

			DoubleLinkedListNode tnode = hmap.get(key);
			tnode.value = value;
			tnode = removeNode(tnode);

			pushFront(tnode);
		}

		else {
			DoubleLinkedListNode newNode = new DoubleLinkedListNode(key,value);

			if (hmap.size() == capa) {
				hmap.remove(tail.key);
				removeNode(tail);
			}

			pushFront(newNode);
			hmap.put(key, newNode);
		}
	}

	void pushFront(DoubleLinkedListNode node) {
		if (head == null) {
			head = tail = node;
		} else {
			node.next = head;
			head.prev = node;
			head = node;
		}
		return;
	}

	DoubleLinkedListNode removeNode(DoubleLinkedListNode node) {
		DoubleLinkedListNode prev = node.prev;
		DoubleLinkedListNode next = node.next;

		if (prev != null) {
			prev.next = next;
		} else {
			head = next;
		}

		if (next != null) {
			next.prev = prev;
		} else {
			tail = prev;
		}

		node.prev = null;
		node.next = null;

		return node;
	}

	class DoubleLinkedListNode {
		int key;
		int value;
		DoubleLinkedListNode prev;
		DoubleLinkedListNode next;

		public DoubleLinkedListNode(int key, int val) {
			this.key = key;
			this.value = val;
			prev = null;
			next = null;
		}

		public DoubleLinkedListNode() {
			value = 0;
			prev = null;
			next = null;
		}
	}

}
时间: 2024-07-30 03:00:54

Leetcode- LRUCache的相关文章

Leetcode:LRUCache四个版本实现

题目 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.s

leetcode LRU Cache

题目链接.实现一个数据结构用于LRU,最近最少使用,O(1)插入和删除.关于LRU的基本知识可参考here. 先推荐JustDoIT的. 下面是我自己实现的. class LRUCache{ public: //146LRU Least Recently Used int LRUsize; struct LRUNode { int key; int value; LRUNode *pre, *next; LRUNode(int x, int y): key(x), value(y), pre(N

【leetcode】:LRU Cache_缓存淘汰算法LRU的设计与实现

内存淘汰算法是一个比较重要的算法,经常会被问道:如果让你设计一个LRU算法你会怎么做?尽可能的保持存取的高效.那么就依着这道算法题对LRU进行一个简单的介绍. 题目地址:https://oj.leetcode.com/problems/lru-cache/ 1.什么是LRU算法?为什么使用LRU? LRU即Least Recently Used的缩写,即最近最少使用的意思.我们知道在内存空间是有限的,那么当内存被数据占满的时候,而需要访问的数据又不在内存中,那么就需要将内存中的最少使用的数据淘汰

[leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高. 广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单.而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为了能在O(1) 时间获得key 需要类hash的结构,在python里,就用字典. 接下来的事情是,我

Java for LeetCode 146 LRU Cache 【HARD】

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

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

LeetCode: LRU Cache [146]

[题目] 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

leetcode之LRU实现

题目: 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.

LeetCode解题报告:LRU Cache

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 retu

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl