leetcode 146.LRU Cache
来源:互联网 发布:淘宝店鞋店名大全 编辑:程序博客网 时间:2024/05/21 15:42
平衡树
题意:
实现一个有容量的键值对的cache.
1.get操作:键在cache里则返回值,不在就返回-1
2.set操作:如果cache已满,则先删除最近最少使用的键值对,再添加新键值对
思路:
键值对,用平衡树(std里是map)或者hash表储存就行了.
关键是那个删除最近最少使用的,我们可以用一个时间戳来记录谁什么时候被使用过,这样时间戳和键其实也是一个键值对.
这样会得到2个新操作:
1.cache满的话,要找到并删除最小的时间戳和键
2.get和set的时候,如果键在cache里,需要更新键的时间戳
我们发现我们既有通过时间戳找键,又要通过键找时间戳,那么我们可以再弄两棵平衡树,储存时间戳和键的双向映射.
"键->时间戳"的平衡树是可以用hash表代替的,但"时间戳->键"的平衡树则不行,因为hash表不能找到最小的时间戳.
那么,每次操作的时候,几乎三棵平衡树都要做相应的查找插入或删除,因此每次操作复杂度为O(logn).
总结:
三棵平衡树:键->值,时间戳->键,键->时间戳
0 0
- LeetCode 146. LRU Cache
- [leetcode] 146.LRU Cache
- leetcode 146. LRU Cache
- Leetcode 146. LRU Cache
- Leetcode - 146. LRU Cache
- Leetcode 146. LRU Cache
- [leetcode] 146. LRU Cache
- Leetcode 146. LRU Cache
- leetcode 146. LRU Cache
- LeetCode-146. LRU Cache
- LeetCode 146. LRU Cache
- LeetCode 146. LRU Cache
- Leetcode 146. LRU Cache
- [LeetCode] 146. LRU Cache
- leetcode 146.LRU Cache
- [LeetCode]146. LRU Cache
- Leetcode 146. LRU Cache
- leetcode 146. LRU Cache
- fastDFS介绍和配置过程
- vim下各类批量替换
- 什么是SOAP?
- standford自然语言处理---introduciton
- socket编程之api
- leetcode 146.LRU Cache
- 第五周项目一(7)1到100连加计算
- Linux workqueue工作原理
- oracle10G/11G官方下载地址集合 直接迅雷下载
- emmet使用简介
- Apache默认index页面
- Android开发——告诉你Adapter应该写在Activity里面还是外面
- 列表
- Mac 下两款 Markdown 编辑器 Mou/MacDown