leetcode_Design and implement a data structure for Least Recently Used (LRU) cache
来源:互联网 发布:中国蓝tv网络直播 编辑:程序博客网 时间:2024/04/30 09:18
/* * Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. * */public class LRUCache{private Map<Integer, MapNode> map = null;private int capacity;private int size;public LRUCache(int capacity){this.capacity = capacity;size = 0;map = new LinkedHashMap<Integer,MapNode>();}public int get(int key){if(map.containsKey(key) == false){return -1;}else {int r = map.get(key).value;//adjust the position of this itemMapNode tmMapNode = map.get(key);map.remove(key);map.put(key, tmMapNode);return r;}}public void set(int key, int value){if(map.containsKey(key)==false){if(size<capacity){map.put(new Integer(key), new MapNode(key, value));size++;}else {//if size capacity is zero , does this still work correctly?//delete the first one which is the least used oneIterator<Entry<Integer, MapNode>> iterator= map.entrySet().iterator();Integer tmp = null;if(iterator.hasNext()){tmp = iterator.next().getKey();//map.remove(tmp); //map.put(new Integer(key), new MapNode(key, value)); }if(tmp != null){map.remove(tmp); map.put(new Integer(key), new MapNode(key, value)); }}}else {//delete this node and change its content then add it,make it the last oneMapNode tmpMapNode = map.get(new Integer(key));map.remove(new Integer(key));tmpMapNode.value = value;map.put(new Integer(key), tmpMapNode);}}public class MapNode{int key;int value;public MapNode(int k, int v){key = k;value = v;}}}
总结延伸://各种容器使用 map hashmap 等 他们操作的复杂度?,这里使用的LinkedHashMap其中数据保持插入顺序,后进的在头部
(notice that java.util.LinkedHashMap uses insertion-order.)
0 0
- leetcode_Design and implement a data structure for Least Recently Used (LRU) cache
- Least Recently Used(LRU) Cache
- LRU (Least Recently Used)
- LRU(Least recently used)
- LRU(Least Recently Used)
- 晦涩的翻译LRU (Least Recently Used) - 最近最少使用?
- LRU(Least Recently Used)缓存介绍与实现
- 请用Java设计一个Least Recently Used (LRU) 缓存
- LRU (Least Recently Used) 算法的Java实现
- 请用Java设计一个Least Recently Used (LRU) 缓存
- Java应用缓存介绍与LRU(Least Recently Used)算法
- LRU(least recently used)内存淘汰旧数据
- python实现简单的LRU(Least Recently Used)
- LRU(Least Recently Used)最近最少使用页面置换算法
- A data structure for n elements and O(1) operations
- 利用LinkedHashMap实现一个简单的LUR(Least Recently Used 近期最少使用算法)的cache的两种方法
- [Seminar Presentation] DCell: A Scalable and Fault-Tolerant Network Structure for Data Centers
- [Python for Data Analysis]Python Basic2--Data Structure and Sequences
- ipv6 全球单播地址,link-local address、site-local address、组播、任意波、EUI-64等地址的理解
- [LeetCode]Evaluate Reverse Polish Notation
- 用VS2010建立空白解决方案
- struts2中的constant配置详解
- python -- type/isinstance
- leetcode_Design and implement a data structure for Least Recently Used (LRU) cache
- RTSP协议介绍
- 破解android 程序的基本步骤
- C++ STL 容器技术 之 list双向链表容器
- iOS 去掉tabaar上面的 一条线
- MYSQL日期函数大全
- 关于分库分表(Mysql篇)
- 百度网盘之android4.2源码下载
- The connection to adb is down, and a severe error has occured.