LeetCode-LRU Cache<未完成>

来源:互联网 发布:中标数据查询网 编辑:程序博客网 时间:2024/06/05 12:42
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/38684641
时间:2014-8-19

题目

LRU Cache

 Total Accepted: 15448 Total Submissions: 112058My Submissions

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.


解法
ArrayList 作为java中的容器,替代了Vector(LeetCode中不允许外来的包)
基本的思路是:用Arraylist作为队列式的Cache
LRU算法有如下情况(SET):
1.当Cache没有Full,如果命中Cache,把命中的页放到第一项。
2.当Cache已经Full,如果未命中,将最不常用的Cache中的元素删除,并把新的一页写入到第一个位置;如果命中,把命中的页放到第一项。
由于使用了ArrayList(size,remove(index),add(),get())
简化为:
命中的话:把命中的项删去,并将命中的页加入到第一项
未命中的话:如果未满,则在第一项加入;否则,先删去最后一项,然后加入第一项。

public class LRUCache {        HashMap<String,String> map;    ArrayList<String> list;    int capacity;         public LRUCache(int capacity) {        this.capacity = capacity;        list = new ArrayList<String>();        map = new HashMap<String,String> ();    }        public int get(int key) {        String k = Integer.toString(key);        if(map.containsKey(k))            return Integer.parseInt(map.get(k));        else             return -1;    }        public void set(int key, int value) {        String k = Integer.toString(key);        String i = Integer.toString(value);        if(list.contains(k)){// has this one            int index = list.lastIndexOf(k);            list.remove(index);            list.add(k);        }        else{            if(capacity == list.size()){ //Cache is full                //last element remove                int lastIndex = list.size()-1;                map.remove(list.get(lastIndex));                list.remove(lastIndex);            }            //add elemnet            list.add(k);            map.put(k , i);        }    }}

出现问题:

Submission Result: Time Limit Exceeded


改变思路:不用ArrayList
第一个反应是用LinkedList(这里有一个pollLast())可以获取并移除最后一个,可以节省一次时间。
但是对于2048的结果还是不好。
public class LRUCache {        HashMap<String,String> map;    LinkedList<String> list;    int capacity;         public LRUCache(int capacity) {        this.capacity = capacity;        list = new LinkedList<String>();        map = new HashMap<String,String> ();    }        public int get(int key) {        String k = Integer.toString(key);        if(map.containsKey(k))            return Integer.parseInt(map.get(k));        else             return -1;    }        public void set(int key, int value) {        String k = Integer.toString(key);        String i = Integer.toString(value);        if(list.contains(k)){// has this one            int index = list.indexOf(k);            list.remove(index);            list.add(k);        }        else{            if(capacity == list.size()){ //Cache is full                //last element remove                String delete = list.pollLast();                map.remove(delete);            }            //add elemnet            list.add(k);            map.put(k , i);        }    }}
只有彻底的改变:






0 0
原创粉丝点击