LRU Cache [LeetCode]
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.
Solution:
 class LRUCache{
 public:
     vector<int> keys;
     unordered_map<int, int> map;
     int size = ;
     LRUCache(int capacity) {
         size = capacity;
     }
     void adjust(int key){
         int idx = -;
         for(int i = keys.size() - ; i >=  ; i --)
             if(keys[i] == key){
                 idx = i;
                 break;
             }
         if(idx == -)
             return;
         keys.erase(keys.begin() + idx);
         keys.push_back(key);
     }
     int get(int key) {
         if(map.find(key) == map.end()){
             return -;
         }else{
             adjust(key);
             return map[key];
         }
     }
     void set(int key, int value) {
         if(map.find(key) != map.end()){
             map[key] = value;
             adjust(key);
             return;
         }
         if(keys.size() >= size ){
             int key_to_erase = keys[];
             keys.erase(keys.begin());
             map.erase(key_to_erase);
         }
         keys.push_back(key);
         map[key] = value;
     }
 };
LRU Cache [LeetCode]的更多相关文章
- LRU Cache leetcode java
		
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
 - [LeetCode] LRU Cache 最近最少使用页面置换缓存器
		
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
 - Java for LeetCode 146 LRU Cache 【HARD】
		
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
 - LeetCode之LRU Cache 最近最少使用算法 缓存设计
		
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
 - 【LeetCode OJ】LRU Cache
		
Problem Link: http://oj.leetcode.com/problems/lru-cache/ Long long ago, I had a post for implementin ...
 - leetcode@ [146] LRU Cache (TreeMap)
		
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
 - [LeetCode]LRU Cache有个问题,求大神解答【已解决】
		
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
 - 【LeetCode】LRU Cache 解决报告
		
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
 - [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
		
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
 
随机推荐
- Cursor use
			
Ref:http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html declare @Id varchar(100),@name ...
 - Leetcode: Maximum XOR of Two Numbers in an Array
			
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
 - android开发中scrollview添加自定义view的滑动显示问题
			
最近做了一个实战用到自定义view,由于view比屏幕大所以想放到scrollview中,如下程序.发现不显示.于是对scrollview进行了研究. <LinearLayout xmlns:a ...
 - objective-c第六章课后练习6
			
题6:接受从终端输入的整数,提取并用英语显示这个数的每一个数字,如932,显示nine three two (题目中注了.这个练习很难)的确有点难,自己想了很久网上也各种搜索.也算是找到参考了 cod ...
 - 使用 Redis 实现排行榜功能 (转载 https://segmentfault.com/a/1190000002694239)
			
排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如"用户积分榜".如果没有实效性一直按照总榜来排,可能榜 ...
 - jq 解析josn字符串
			
1. var obj = jQuery.parseJSON("${ruleModel.rules}"); 2. var obj = eval("("+" ...
 - 导入maven工程遇见的问题【问题】
			
原工程是一个基于websocket的maven工程(源工程:http://www.cnblogs.com/xdp-gacl/p/5193279.html),把工程导入eclipse后报错.
 - 用xib自定义UITableViewCell
			
1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...
 - 用andtoid studio获取天气数据并解析适配
			
1.申请拿到数据 可以用“聚合数据” 2.在android studio中导入需要的jar包 复制—>app—>libs—>粘贴—>右击—>Add As Library… ...
 - asp.net 捕获throw
			
<script type="text/javascript"> function pageLoad() { Sys.WebForms.PageRequestManage ...