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. BT5更新源

    感谢http://blog.csdn.net/seaos/article/details/7064136终于找到更新成功的源了 下面简单介绍步骤吧 gedit /etc/apt/sources.lis ...

  2. java操作数据库出现(][SQLServer 2000 Driver for JDBC]Error establishing socket.)的问题所在即解决办法

    在进行jdbc直接操作数据库时    : 我们需要对该工程进行一次导包(引入"msbase.jar" "mssqlserver.jar" "msuti ...

  3. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

  4. JQ添加移除css样式

    1. addClass() - 添加CSS类 $("#target").addClass("newClass"); //#target 指的是需要添加样式的元素 ...

  5. PHP生成word的三种方式

    摘要: 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像在博客园发表博客只要是标题带PHP的貌似点击量都不是很高(哥哥我标题还是带上PHP了),不知道为什么,估计 ...

  6. web页面动态加载UserControl,并调用用户控件中的方法来初始化控件

    1,HTML页 头部注册: <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPre ...

  7. [转]我来Hacking JDBC,你并不需要它

    我们喜欢和JDBC打交道,以前从未有人这样说过.很严肃的说,JDBC是一个非常优秀的API.这可能是现在Java能够成为一个受欢迎的平台的重要原因之一. 在JDK1.1之前,ODBC出现之前(很久之前 ...

  8. Ubuntu 设置su密码

    如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名:  这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...

  9. 图形界面报错“已拒绝X11转移申请”的解决方法

    今天想通过本机给虚拟机起x-manager图形界面的时候报出 解决办法: 1.原来X11 forwarding依赖“xorg-x11-xauth”软件包,所以必须先安装“xorg-x11-xauth” ...

  10. curl,chkconfig

    1. Linux系统服务管理 工具ntsysv 类似图形界面管理工具,如果没有该命令使用 yum install -y ntsysv 安装 常用服务:crond, iptables, network, ...