由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道, leetcode题.
相关主题
LRU cache 超时, 大家帮忙看看问道关于LRU的题目
菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。求leetcode LRU Java 解法
LRU cache 超时Tripadvisor面筋
LRU的多线程版本,这个答案有问题吗谁来解释下hashtable的iterator是怎么实现的
LRU cache 问题Given an int array and an int value. Find all pairs in arr
写的LRU通不过大数据,帮忙看看A面经
关于用STL实现LRU cache发个evernote的code challenge
LRU Cache, 请问, 如果我这样写,错误在哪里?为什么会time limit exceeded? 谢谢Tripadvisor 面经
相关话题的讨论汇总
话题: key话题: int话题: head话题: cacheentry
进入JobHunting版参与讨论
1 (共1页)
f**********e
发帖数: 288
1
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.
这道小伙伴们都用int, 但是问题又没明说是用int. 该怎么样理解呢.generic?
x*******1
发帖数: 28835
2
就是个simulator. 选java,给的test case都是int。
f**********e
发帖数: 288
3
多谢, 兄台.

【在 x*******1 的大作中提到】
: 就是个simulator. 选java,给的test case都是int。
s**x
发帖数: 7506
4
贴下代码吧,这个还真是高频题。
w****3
发帖数: 110
5
我贴一个,另外可以直接用linkedhashmap做,设为accessOnly直接用
removeeldestentry,只要几行就能写完。下面的是自己用doubleLinkedList做的。
public class LRUCache {
HashMap data = new HashMap DoubleLinkedListNode>();
int cap;
int count = 0;
DoubleLinkedListNode head = null;
DoubleLinkedListNode helper;
public LRUCache(int capacity) {
cap = capacity;
helper = new DoubleLinkedListNode(-1,-1);
head = helper;
}

public int get(int key) {
if (data.containsKey(key)) {
//move double LinkedList to head
DoubleLinkedListNode n = data.get(key);
if (!(n == head)) { // if it is the head, do nothing
n.pre.next = n.next;
n.next.pre = n.pre;
head.next = n;
n.pre = head;
n.next = null;
head = n;
}
return n.val;
}
return -1;
}

public void set(int key, int value) {
if (data.containsKey(key)) {
//move double LinkedList to head
DoubleLinkedListNode n = data.get(key);
if (!(n == head)) { // if it is the head, do nothing
n.pre.next = n.next;
n.next.pre = n.pre;
head.next = n;
n.pre = head;
n.next = null;
head = n;
}
n.val = value;
}
else {
if (count == cap) { // remove the tail
data.remove(helper.next.key);
if (helper.next == head) {// if only one element;
head = helper;
helper.next = null;
}
else {
helper.next = helper.next.next;
helper.next.pre = helper;
}
count--;

}
DoubleLinkedListNode n = new DoubleLinkedListNode(key, value);
data.put(key, n);
head.next = n;
n.pre = head;
head = n;
count++;
}
}

public class DoubleLinkedListNode {
DoubleLinkedListNode pre;
DoubleLinkedListNode next;
int key;
int val;
public DoubleLinkedListNode(int k, int v) {
key = k;
val = v;
pre = null;
next = null;
}
}
}
s**x
发帖数: 7506
6
java 不熟, but I suspect you need to use next/pre etc for doublelinkedlist,
that is like C, not even c++.

【在 w****3 的大作中提到】
: 我贴一个,另外可以直接用linkedhashmap做,设为accessOnly直接用
: removeeldestentry,只要几行就能写完。下面的是自己用doubleLinkedList做的。
: public class LRUCache {
: HashMap data = new HashMap: DoubleLinkedListNode>();
: int cap;
: int count = 0;
: DoubleLinkedListNode head = null;
: DoubleLinkedListNode helper;
: public LRUCache(int capacity) {

f**********e
发帖数: 288
7
我也贴一个, 但最复杂的case没过, 有空在查查. 请大家指正.
public class LRUCache {

int capacity;
LinkedList list = new LinkedList();
HashMap map = new HashMap();

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {

if(!map.containsKey(key))
return -1;
CacheEntry c = map.get(key);
int val = c.val;
moveToHead(c);
return val;

}

public void set(int key, int value) {
CacheEntry c = new CacheEntry(key, value);
if(map.containsKey(key)){
moveToHead(c);
} else{
if(list.size() == capacity){
list.remove(capacity - 1);
map.remove(key);
setHead(key, c);
} else {
setHead(key, c);
}
}
}
public void moveToHead(CacheEntry c){
list.remove(c);
list.add(0, c);
}
public void setHead(int key, CacheEntry c){
list.add(0, c);
map.put(key, c);
}


public class CacheEntry{
int val;
int key;
public CacheEntry(int val, int key){
this.val = val;
this.key = key;
}
}
}
w****3
发帖数: 110
8
额,那应该怎么实现呢?似乎java里面没有已经写好的doubly linked list?

doublelinkedlist,

【在 s**x 的大作中提到】
: java 不熟, but I suspect you need to use next/pre etc for doublelinkedlist,
: that is like C, not even c++.

1 (共1页)
进入JobHunting版参与讨论
相关主题
Tripadvisor 面经LRU cache 问题
贡献一个 一个L家的店面题目写的LRU通不过大数据,帮忙看看
LRU C++过不了关于用STL实现LRU cache
类似LRU Cache的题应该怎么练习?LRU Cache, 请问, 如果我这样写,错误在哪里?为什么会time limit exceeded? 谢谢
LRU cache 超时, 大家帮忙看看问道关于LRU的题目
菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。求leetcode LRU Java 解法
LRU cache 超时Tripadvisor面筋
LRU的多线程版本,这个答案有问题吗谁来解释下hashtable的iterator是怎么实现的
相关话题的讨论汇总
话题: key话题: int话题: head话题: cacheentry