【leetcode】LRU Cache
题目简述:
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.
解题思路:
用一个队列存放当前的元素,用字典结构表示cache
#coding=utf-8
class LRUCache:
# @param capacity, an integer
def __init__(self, capacity):
self.cap = capacity
self.cache = {}
self.sta = []
# @return an integer
def get(self, key):
if key in self.cache:
self.sta.remove(key)
self.sta.append(key)
return self.cache[key]
else:
return -1
# @param key, an integer
# @param value, an integer
# @return nothing
def set(self, key, value):
if self.cap == len(self.cache) and (key not in self.cache):
self.cache.pop(self.sta[0])
self.sta.pop(0)
if key in self.cache:
self.sta.remove(key)
self.sta.append(key)
else:
self.sta.append(key)
self.cache[key] = value
【leetcode】LRU Cache的更多相关文章
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【Leetcode】 LRU Cache实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【Leetcode146】LRU Cache
问题描述: 设计一个LRU Cache . LRU cache 有两个操作函数. 1.get(key). 返回cache 中的key对应的 val 值: 2.set(key, value). 用伪代码 ...
- 【leetcode】LRU
import java.util.HashMap; import java.util.Map; public class LRUCache { private int capacity; privat ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- 借助JavaScript中的时间函数改变Html中Table边框的颜色
借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...
- Google数据交换格式:ProtoBuf
Protocol Buffer ProtocolBuffer是Google公司的一个开源项目,用于结构化数据串行化的灵活.高效.自动的方法,有如XML,不过它更小.更快.也更简单.你可以定义自己的数据 ...
- EF Core 杂记
本系列文章,将介绍本人在学习和使用EF Core的过程中的收获与心得. 或许有的地方讲的错误 欢迎大家批评指出. 1.EF Core 数据库迁移(Migration)
- Android之弹出/隐藏系统软键盘
Android弹出/隐藏系统软键盘的代码如下: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT ...
- Java的输入方式总结
写java代码的时候,经常会遇到的情况就是输入输错了怎么办?大部分想的是用一个if判断,但是用if判断的话我们就无法让用户再次输入,因为if语句程序执行后就会直接退出程序.因此要想实现循环就要用whi ...
- Excel——OFFSET函数
1.首先看下offset函数的参数设置: 说明:height,width表面它的返回值可以是一个数组,而并非一个值.这样,它就可以用于数据有效性等. 2.使用offset实现转置: 3.offset函 ...
- Apache服务器安装过程及问题的解决(for windows system32bit)
在使用Hbuilder设计网站时,在制作本站搜索时,用到了Php文件,而Hbuilder的内置web服务器不支持php的解析, 所以需要安装配置外部服务器,有多个选择,我安装的apache服务器,并遇 ...
- rpc-1-OSI模型
rpc-1-OSI模型 第一部分,网络7层协议 1. OSI模型: 开放通信系统互联网参考模型,是国际标准化组织(ISO),提出的一个,试图使各种计算机在世界范围内互连为网络的模式.(遵循这个模式,计 ...
- json格式化工具
1.JsonViewer 可对json数据进行查看.格式化.编辑...... 2.在线工具 http://json.parser.online.fr/
- angularJS实践过程中出现的问题总结
同名服务 在一次项目里,之前是同事写的.我有一次在异步获取服务器上的数据时,习惯把api地址写在一个服务Store里,但是程序总是返回Store.api.get()里的get is undefined ...