你有一排书架,有空时会拿些书来看,经常性会买些新书。无奈书架容量有限,当新买的书放不下时,需要一个策略将旧书淘汰。

LRU(最近最少使用)缓存淘汰机制正合适。

1)新买的书放在最左侧。

2)最近常看的书也放在最左侧。

久而久之,越往右边的书越是长时间没看,当有新书时,就从右侧淘汰起。Perfect。

下面一起来动手实现这个算法。附加条件只有一个,要在 O(1) 的时间复杂度内完成操作。

查找操作能在O(1)的数据结构: 哈希

增、删除操作能在O(1)的数据结构:链表

所以考虑哈希 + 单链 或哈希 +双链表。不过现实中,双链表的使用场景远多于单链表。

原因大家仔细考虑下,欢迎留言

class ListNode:
def __init__(self, key = None, value =None):
self.key = key
self.value = value
self.prev = None
self.next = None class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.hashmap = {} # 哨兵
self.head = ListNode()
self.tail = ListNode() self.head.next = self.tail
self.tail.prev = self.head def move_node_to_tail(self, key): node = self.hashmap[key]
node.prev.next = node.next
node.next.prev = node.prev node.prev = self.tail.prev
node.next = self.tail
self.tail.prev.next = node
self.tail.prev = node def get(self,key:int) -> int:
if key in self.hashmap:
self.move_node_to_tail(key)
res = self.hashmap.get(key, -1)
if res == -1:
return res
else:
return res.value def put(self, key:int, value:int)->int:
if key in self.hashmap:
self.hashmap[key].value= value
self.move_node_to_tail(key)
return
if len(self.hashmap) == self.capacity:
self.hashmap.pop(self.head.next.key)
self.head.next = self.head.next.next
self.head.next.prev = self.head new = ListNode(key,value)
self.hashmap[key] = new
new.prev = self.tail.prev
new.next = self.tail
self.tail.prev.next = new
self.tail.prev = new

LeetCode - LRU怎么将书架上的旧书完美淘汰呢的更多相关文章

  1. 洛谷P2125图书馆书架上的书 题解报告

    题目描述 图书馆有n个书架,第1个书架后面是第2个书架,第2个书架后面是第3个书架……第n-1个书架后面是第n个书架,第n个书架后面是第1个书架,第i个书架上有b[i]本书.现在,为了让图书馆更美观, ...

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  4. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  5. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  6. 面试挂在了 LRU 缓存算法设计上

    好吧,有人可能觉得我标题党了,但我想告诉你们的是,前阵子面试确实挂在了 RLU 缓存算法的设计上了.当时做题的时候,自己想的太多了,感觉设计一个 LRU(Least recently used) 缓存 ...

  7. leetcode LRU缓存机制(list+unordered_map)详细解析

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (key) 存 ...

  8. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  9. leetcode - 位运算题目汇总(上)

    最近在看位运算的知识,十分感叹于位运算的博大精深,正好leetcode有 Bit Manipulation 的专题,正好拿来练练手. Subsets 给出一个由不同的数字组成的数组,枚举它的子数组(子 ...

随机推荐

  1. 贪心+dp

    贪心+dp 好多题都是这个思想, 可以说是非常重要了 思想一: 在不确定序列无法dp的情况下, 我们不妨先假设序列已经选定, 而利用贪心使序列达到最优解, 从而先进行贪心排序, 在进行dp选出序列 思 ...

  2. 解决reportNG中文乱码(转:http://www.it610.com/article/3626590.htm)

    1.下载reportng源码      git clone  https://github.com/dwdyer/reportng.git 2.修改AbstractReporter.java      ...

  3. HTML简单介绍(个人角度)

    之前对HTML的理解:HTML主要是标签组成,一对标签组成一个位置,在响应的位置内填写对应的内容. 1.编译工具 [ ] HTML需要编译工具?txt文档改后缀双击运行. [ ] 了解前端了一下前端, ...

  4. CentOS7安装配置MariaDB(mysql)数据主从同步

    CentOS7安装MariaDB并配置主从同步 环境声明: 防火墙firewalld及SElinux均为关闭状态 主库节点:192.168.0.63 从库节点:192.168.0.64 配置主库节点: ...

  5. C++中利用迭代器删除元素会发生什么?

    转自:https://blog.csdn.net/yf_li123/article/details/75003425#comments   (1)对于关联容器(如map,set,multimap,mu ...

  6. [BJWC2008] Gate Of Babylon

    题目链接 容斥+隔板法+Lucas定理 #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int n,m, ...

  7. RegexKitLite库的使用

    首先在官网:  http://regexkit.sourceforge.net/下载RegexKitLite.h和RegexKitLite.m两个文件 将其添加到工程目录下 取消自动引用计数ARC 手 ...

  8. Git+码云安装

    注册码云 1.1 下载git https://git-scm.com 1.2 安装 git安装一直next 下一步就行 1.3 测试 1.4 git原理

  9. Python time strptime()方法 时间操作

    描述 Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组. 语法 strptime()方法语法: time.strptime(string[, format ...

  10. centos 6.x 编译安装 pgsql 9.6

    文章结构如下: 一. 环境配置 1. 配置防火墙 查看IPTABLES 当前状态与关闭过程 chkconfig --list|grep iptables 关闭iptables service ipta ...