题目如下:

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 given index to be equal to val.
  • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
  • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_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 5

Constraints:

  • 1 <= length <= 50000
  • At most 50000 calls will be made to setsnap, and get.
  • 0 <= index < length
  • 0 <= snap_id < (the total number of times we call snap())
  • 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的更多相关文章

  1. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  3. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  5. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  6. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

  7. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  8. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  9. 【LeetCode】905. Sort Array By Parity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...

随机推荐

  1. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  2. PHP is_file() 函数

    is_file() 函数检查指定的文件名是否是正常的文件. 语法is_file(file)参数 描述file 必需.规定要检查的文件.说明如果文件存在且为正常的文件,则返回 true. 提示和注释 注 ...

  3. Servlet 响应 响应相关与重定向 请求 获取表单数据2种方法

    一.HttpServletResponse  (响应) 包括下面三个: 1.响应消息行  HTTP/1.1  200 OK 200是HTTP状态码, 代表请求已成功. (查httpservletres ...

  4. 【MM系列】SAP MM模块-配置PO的创建时间

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-配置PO的创建时间 ...

  5. MySQL学习-入门语句以及增删查改

    1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...

  6. (转载)gcc编译选项总结

    转载自:https://blog.csdn.net/gatieme/article/details/21389603 常用编译选项 gcc and g++分别是gnu的c & c++编译器 g ...

  7. java中的命名规则

    转载自:http://growstep.diandian.com/post/2011-08-17/3989094 1.类名首字母应该大写.属性(成员变量).方法.对象变量以及所有标识符(如形式参数.实 ...

  8. python下对文件的操作(非目录)

    总文件夹 子文件夹01 文档01.txt-------------------------------------------------------------------------------- ...

  9. adb 连接 mumu 模拟器

    [win版]adb connect 127.0.0.1:7555adb shell [mac版] adb kill-server && adb server && ad ...

  10. Java IO NIO详细讲解

    1.IO Java IO概述 2.NIO Java NIO浅析