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.

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.

【题意】

实现LRU策略

取相应的key-value

插入key-value是,需要删除LRU的item

【思路】

维护一个Map记录对应的<key, value>对

为了模拟key的访问先后关系,需要维护一个访问次序列表,越靠后的节点,访问时间距当前时间越短

而在insert或者访问key的时候,需要从列表中找到对应的key,并把它调整到列表为。

这里遇到两个问题,一个是查找,另一个是移动到末尾

如果使用顺序表,查找O(n),移动O(n),在cache规模很大时时间代价过高

因此这里使用双向链表来处理

【代码】

struct Node{
    int key;
    int val;
    Node*prev;
    Node*next;
    Node(int k, int v): key(k), val(v){
        prev=NULL; next=NULL;
    }
};

class LRUCache{
private:
	Node* head;
	Node* tail;
	int capacity;
	map<int, Node*>cache;
public:
    LRUCache(int capacity) {
        this->head = NULL;
		this->tail = NULL;
		this->capacity = capacity;
    }

	void move2tail(Node* node){
		if(node==tail)return;
		if(node==head){
			head = node->next;
			head->prev=NULL;
			tail->next=node;
			node->prev=tail;
			tail=node;
			tail->next=NULL;
		}
		else{
			node->prev->next = node->next;
			node->next->prev = node->prev;
			tail->next=node;
			node->prev=tail;
			tail=node;
			tail->next=NULL;
		}
	}

    int get(int key) {
        if(this->cache.find(key)==this->cache.end())return -1;
		move2tail(this->cache[key]);
		return this->cache[key]->val;
    }

    void set(int key, int value) {
        if(this->cache.find(key)==this->cache.end()){//cache中还没有
			if(this->capacity==0){//cache已经满了
				//删除头结点
				this->cache.erase(head->key);
				head=head->next;
				if(head)head->prev=NULL;
				else tail=NULL;
			}
			else{//cache还没满
				this->capacity--;
			}
			//添加新节点
			Node* newNode=new Node(key, value);
			this->cache[key]=newNode;
			if(tail){
				tail->next=newNode;
				newNode->prev=tail;
				tail=newNode;
				tail->next=NULL;
			}
			else{
				head=tail=newNode;
			}
		}
		else{//cache中已经有了
			this->cache[key]->val = value;
			move2tail(this->cache[key]);
		}
    }
};

LeetCode: LRU Cache [146],布布扣,bubuko.com

时间: 2024-10-09 20:52:49

LeetCode: LRU Cache [146]的相关文章

[leetcode]LRU Cache (python)

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

[leetcode]LRU Cache @ Python

原题地址:http://oj.leetcode.com/problems/lru-cache/ 题意:设计LRU Cache 参考文献:http://blog.csdn.net/hexinuaa/article/details/6630384 这篇博文总结的很到位.   https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LRUCache.py 代码参考的github人写的,思路非常清晰,写的也很好. Cache简介: Ca

[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.

[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.

LeetCode——LRU Cache

Description: 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 r

[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

LeetCode – LRU Cache (Java)

Problem 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

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 Golang

package main import( "fmt" ) type Node struct { Key string Val string Pre *Node Next *Node } type DLinkedList struct { Head *Node Tail *Node } func (self *DLinkedList) IsEmpty() bool { if self.Head == nil && self.Tail == nil { return tru