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的更多相关文章

  1. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  2. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  3. LeetCode OJ:LRU Cache(最近使用缓存)

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  4. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  5. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  6. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  7. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  8. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  9. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

随机推荐

  1. (18)odoo规范

    * 约定    # 命名会用  蛇形式或驼峰式        todo_task_stage 蛇形式        class TodoTask 驼峰式      变量名还是蛇形居多, 类名和方法名驼 ...

  2. C语言知识整理(2):volatile与register

    1.volatile volatile是易变的,不稳定的意思,volatile是关键字,是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统.硬件或者其他线程等,遇到这个关 ...

  3. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. kindeditor 操作时同步到textarea

    KindEditor.ready(function(K) { editor1 = K.create('textarea[name="sponsor"]', { resizeType ...

  5. redhat enterprixe 5.0 DNS 服务配置与管理

    一.了解DNS相关概念 DNS是一个分布式数据库,在本地负责控制整个分布式数据库的部分段,每一段中的数据通过客户机/服务器模式在整个网络上存取.通过采用复制技术和缓存技术使得整个数据库稳定可靠的同时, ...

  6. 经典SQL语句大全.doc

    1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数据的 ...

  7. intel的网卡故障

    现象: 机器键盘接入,敲入无反应:机器无法ping通,整台机器假死状态. 查看message的日志,日志为如下内容: Aug :: TSMIS-CF kernel: ::19.0: eth0: Det ...

  8. java 面向对象编程-- 第15章 集合框架

    1.  集合特点:元素类型不同.集合长度可变.空间不固定 2.  java中对一些数据结构和算法进行了封装即集合.集合也是一种对象,用于存储.检索.操作和传输对象. 3.  JCF(Java Coll ...

  9. UVa 11427 - Expect the Expected

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. IIS CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问

    解决方案:给Windows下temp文件添IIS_USERS权限即可