【LeetCode OJ】LRU Cache
Problem Link:
http://oj.leetcode.com/problems/lru-cache/
Long long ago, I had a post for implementing the LRU Cache in C++ in my homepage:
http://www.cs.uml.edu/~jlu1/doc/codes/lruCache.html
So this time, I am trying to use Python to implement a LRU Cache.
However, the result is Time Limit Exceeded.....
My previous post is of C++ template version, but for this problem, the key and value are both integer, so I had a simpler version in C++ here.
I will keep trying to pass the test using python... damn efficiency...
#include <iostream>
#include <vector>
#include <map> using namespace std; struct LRUCacheEntry
{
int key;
int data;
LRUCacheEntry* prev;
LRUCacheEntry* next;
}; class LRUCache{
private:
map<int, LRUCacheEntry*> _mapping;
vector<LRUCacheEntry*> _freeEntries;
LRUCacheEntry * head;
LRUCacheEntry * tail;
LRUCacheEntry * entries; public:
LRUCache(int capacity) {
entries = new LRUCacheEntry[capacity];
for (int i=0; i<capacity; i++)
_freeEntries.push_back(entries+i);
head = new LRUCacheEntry;
tail = new LRUCacheEntry;
head->prev = NULL;
head->next = tail;
tail->next = NULL;
tail->prev = head;
} ~LRUCache()
{
delete head;
delete tail;
delete [] entries;
} int get(int key) {
LRUCacheEntry* node = _mapping[key];
if(node)
{
detach(node);
attach(node);
return node->data;
}
else return -1;
} void set(int key, int value) {
LRUCacheEntry* node = _mapping[key];
if(node)
{
// Move the node to the head and update the value
detach(node);
node->data = value;
attach(node);
}
else{
if ( _freeEntries.empty() )
{
// Get the last node
node = tail->prev;
// Move it to the head and update (key, value)
// Update the map
detach(node);
_mapping.erase(node->key);
node->data = value;
node->key = key;
_mapping[key] = node;
attach(node);
}
else{
node = _freeEntries.back();
_freeEntries.pop_back();
node->key = key;
node->data = value;
_mapping[key] = node;
attach(node);
}
}
} private:
void detach(LRUCacheEntry* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void attach(LRUCacheEntry* node)
{
node->next = head->next;
node->prev = head;
head->next = node;
node->next->prev = node;
}
};
【LeetCode OJ】LRU Cache的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ:LRU Cache(最近使用缓存)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
随机推荐
- Java NIO框架Mina、Netty、Grizzly介绍与对比
Mina:Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用 ...
- oracle序列为什么不是从1开始
问题原因: ·当我们使用序列作为插入数据时,如果使用了“延迟段”技术,则跳过序列的第一个值 ·Oracle从 11.2.0.1版本开始,提供了一个“延迟段创建”特性: 即 当我们创建了新的表(tabl ...
- UI-简答的BOL的取值塞值
不知道从什么时候开始,习惯用BOL MODEL来做一些东西的了.某个项目开始正式接触标准主数据的时候,开始了用MAINTAIN BAPI和BUPA的一些FM.然后在一段时间内是以此类的FM来开发的.B ...
- State模式的经典应用场景:订单处理(c#实现)
State模式在对象内部状态发生变化的时候,改变自身的行为,这通常是通过切换内部状态对象实现的,对象将自身在各个状态的行为推给了状态对象,从而解开了行为与对象的依赖. 场景描述 在经典的订单处理场景中 ...
- BootStrap 最佳资源合集(转)
witter BootStrap是一款优秀的前端的框架,称得上是前端的一个框架利器.Web前端开发者每天都在与HTML.CSS.JavaScript打交道,然 而不少人都是在周而复始的写模板.样式和交 ...
- jdk版本比较
JDK各个版本的新特性 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升级的新特性,这样才能循序渐进的学好一门语言.今天先为大家介绍一 ...
- async 和await
这个是.NET 4.5的特性,所以要求最低.NET版本为4.5. 看很多朋友还是使用的Thread来使用异步多线程操作,基本上看不见有使用Async.Await进行异步编程的.各有所爱吧,其实都可以. ...
- 最新电Call记录统计-full hash join用法
declare @time datetime set @time='2016-07-01' --最新的电Call记录统计查询--SELECT t.zuoxi1,t.PhoneCount,t.Phone ...
- treap 1286郁闷的出纳员.cpp
#include<cstdio>#include<cstdlib>#include<ctime>struct shu{ int l,r,sum,zhi,dui;}a ...
- LA 5059 - Playing With Stones
博弈 SG 由于每个a太大,没有办法递推,但是可以找规律 a为偶数 SG(a)=a/2 a为奇数 SG(a)=SG(a/2) 代码: #include <iostream> #inc ...