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

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. 【VS开发】raw socket 的例子

    raw socket 的例子 一. 摘要    Raw Socket: 原始套接字    可以用它来发送和接收 IP 层以上的原始数据包, 如 ICMP, TCP, UDP... int sockRa ...

  2. mysql——单表查询——其它整理示例01

    create database see; use database see; drop database sww; ========================================== ...

  3. 模板中用url_for的好处

    from flask import Flask,render_template app = Flask(__name__) @app.route('/') def index(): return re ...

  4. Linux小技巧:du -sh * —— 查询文件目录大小

    du -ach * #这个能看到当前目录下的所有文件占用磁盘大小和总大小 du -sh #查看当前目录总大小 du -sh * #查看所有子目录大小 du -sh ./* #查看当前目录下所有文件/文 ...

  5. Spring JdbcTemplate 和 NamedParameterJdbcTemplate 使用

    1.简单介绍 DAO层 的一般使用常见的是MyBatis 和 Hibernate,但是Hibernate是重量级的,而且学习成本较高,Mybatis 需要编写大量配置文件及接口文件,对于简单的项目应用 ...

  6. 插座-网络问题-ESP8266

    //ATK-ESP8266模块测试主函数,检查WIFI模块是否在线 void atk_8266_test(void) { ))//检查WIFI模块是否在线 { atk_8266_quit_trans( ...

  7. C++中函数模板的概念和意义

    1,对泛型编程进行学习,泛型编程是实际工程开发中必用的技术,大型公司的通用 库都是采用泛型编程的技术完成的,C++ 中支持泛型编程技术,C++ 中的函数  模板和类模板就是 C++ 中泛型编程技术,本 ...

  8. selenium之京东商品爬虫

    #今日目标 **selenium之京东商品爬虫** 自动打开京东首页,并输入你要搜索的东西,进入界面进行爬取信息 ``` from selenium import webdriver import t ...

  9. php Excel 导入

    php Excel 导入 public function storeSql() { $file = input('file.excel'); $path = ROOT_PATH . 'public' ...

  10. 使用python的kazoo模块连接zookeeper实现最基本的增删改查

    kazoo的官方文档地址:https://kazoo.readthedocs.io/en/latest/index.html #!/usr/bin/env python # -*- coding: u ...