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. jsp之 ---- 页面重定向和请求转发(笔记之深度说明)

    1.  HttpServletResponse对象的sendRedirect(String location)方法称作重定向. 如果location地址前面加上“/”,则表示  相对于Servlet容 ...

  2. nyoj------布线问题(kruscal+求最小值)

    布线问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...

  3. Oracle 表死锁 解决

    问题:更新的Update语句一直在更新 卡在执行update语句的地方. 清除的方法: Oracle表死锁解除   我是在plsql中处理  1.先查询  select * from v$locked ...

  4. Sui 弹框固定

    SUI是一套基于bootstrap开发的前端组件库,同时它也是一套设计规范,可以非常方便的设计和实现精美的页面,是一个简单易用.功能强大的UI库.自己在使用sui过程之中,总是忘记它的一些Api,今天 ...

  5. JavaScript EventLoop

    转自:http://cek.io/blog/2015/12/03/event-loop/ What is JavaScript What is JavaScript anyway? Some word ...

  6. linux 安装 php

    1.  libxml2安装           tar -zxvf libxml2-2.6.26.tar.gz cd libxml2-2.6.26 ./configure  --prefix=/usr ...

  7. mysql用户备份与修复

    1.修复表repair table tb1 [use frm]; #红色部分代表可添加也可不加, 2.show variables like '%timeout%';  #查询关键字 3. 更改数据, ...

  8. js打印对象(object)

    function printObject(obj){//obj = {"cid":"C0","ctext":"区县"}; ...

  9. 摄像机导致的粒子效果混乱出错变成贴图sprite显示在镜头前

    只要把出错的摄像机记的标签改成maincamera问题就消失了!! 我之前一直以为是烘培导致的问题!

  10. SQL Server 自定义字符串分割函数

    一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数)   create function Func_StrArrayL ...