【leetcode】1146. Snapshot Array
题目如下:
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length)
initializes an array-like data structure with the given length. Initially, each element equals 0.void set(index, val)
sets the element at the givenindex
to be equal toval
.int snap()
takes a snapshot of the array and returns thesnap_id
: the total number of times we calledsnap()
minus1
.int get(index, snap_id)
returns the value at the givenindex
, at the time we took the snapshot with the givensnap_id
Example 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5Constraints:
1 <= length <= 50000
- At most
50000
calls will be made toset
,snap
, andget
.0 <= index < length
0 <= snap_id <
(the total number of times we callsnap()
)0 <= val <= 10^9
解题思路:用snap_id记录当前snap的版本号,每次set操作的时候,把(snap_id,index)当做key存入字典中;get操作的时候,把当前(snap_id,index)当做key在字典中查找,如果key存在则返回对应的值,如果不存在就把snap_id减去1继续查找,直到找到值或者snap_id为0为止。
代码如下:
class SnapshotArray(object): def __init__(self, length):
"""
:type length: int
"""
self.length = length
self.dic = {}
self.snap_id = 0 def set(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
if index >=0 and index < self.length:
self.dic[(index,self.snap_id)] = val def snap(self):
"""
:rtype: int
"""
self.snap_id += 1
return self.snap_id - 1 def get(self, index, snap_id):
"""
:type index: int
:type snap_id: int
:rtype: int
"""
while snap_id >= 0:
if (index,snap_id) in self.dic:
return self.dic[(index,snap_id)]
else:
snap_id -= 1
return 0 # Your SnapshotArray object will be instantiated and called as such:
# obj = SnapshotArray(length)
# obj.set(index,val)
# param_2 = obj.snap()
# param_3 = obj.get(index,snap_id)
【leetcode】1146. Snapshot Array的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【LeetCode】905. Sort Array By Parity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...
随机推荐
- Collector解读以及自定义
一.Collector接口解读: Collector接口解读: public interface Collector<T, A, R> { Supplier<A> suppli ...
- Linux_ServicesManagement_RHEL7
目录 目录 Network Manager RHEL7的服务管理systemctl指令 服务的启动停止重载重启 服务的分类 指令选项 Network Manager 注意:network servic ...
- object Object {} any unknown
object: 除了primitive(boolean null number string undefined bigint symbol)的类型 Object: Object和any很像 ,Obj ...
- GitHub入门(一)GIT配置与Hexo博客搭建
首先安装配置Git环境,由于本人使用Windows操作系统所以从msysgit.github.io下载msysGit Windows版本,安装.(Mac一般自带Git) 安装的时候一般使用默认选项,其 ...
- multiple datasource config
Hi Harshit S. project structure: multiple datasource config as follows: step 1: step 2:add a datasou ...
- MAVEN打包时跳过Junit测试
我们知道,通常情况下使用maven package命令打包时,会自动执行test包下的各个单元测试. 这是因为spring-boot-maven-plugin插件已经集成了maven-surefire ...
- 安卓手机上传同一张图片第二次不触发onchange
清空上一次file内部的值 <script type="text/javascript"> var file = document.getElementById(&q ...
- vue仿阿里云后台管理(附加阿里巴巴图标使用)
先看下页面截图,在线演示地址http://aliadmin.zengjielin.top 下面有开源的代码 页面分成三大部分头部,头部菜单栏,侧边菜单栏,右侧内容栏. 现在我们担心的是怎么使用侧边栏. ...
- P1622释放囚犯
这是一道绿题,是一道让人想用贪心但却是区间DP的题目,难倒了我这个蒟蒻. 这个题其实仔细观察是类似于石子合并的!合并石子的代价便是肉的数量,求最小代价.所以我们设dp[i][j]为释放第i个到第j个所 ...
- oracle管理基础知识
1.oracle的安装 win下 linux下 2.内存和后台进程=实例 为何将oracle做的如此复杂呢 1.内存: --提高查询速度 --提升处理数据的速度 2.后台进程 --为了完成特定的服务, ...