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

Solution0:

class SnapshotArray:

    def __init__(self, length: int):
self.array = [[0] for i in range(length)]
self.indx = [[0] for i in range(length)]
self.snaps = 0 def set(self, index: int, val: int) -> None:
snaps = self.snaps
self.indx[index].append(snaps)
self.array[index].append(val) def snap(self) -> int:
self.snaps += 1
return self.snaps - 1 def get(self, index: int, snap_id: int) -> int: if not self.indx[index]:
return 0
left = 0
right = len(self.indx[index]) - 1
while (left < right):
mid = left + (right - left)//2
if (self.indx[index][mid] > snap_id):
right = mid
else:
left = mid + 1 if self.indx[index][left] > snap_id:
return self.array[index][left-1]
else:
return self.array[index][left]

Solution1:

class SnapshotArray(object):

    def __init__(self, length):
"""
:type length: int
"""
self.snaps = 0
self.store = dict()
self.store[0] = dict() def set(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
self.store[self.snaps][index] = val def snap(self):
"""
:rtype: int
"""
self.snaps += 1
a = (self.store[self.snaps -1]).copy()
self.store[self.snaps] = a
return self.snaps -1 def get(self, index, snap_id):
"""
:type index: int
:type snap_id: int
:rtype: int
"""
if index in self.store[snap_id]:
return self.store[snap_id][index]
else:
return 0

1146. Snapshot Array的更多相关文章

  1. LeetCode 1146. Snapshot Array

    原题链接在这里:https://leetcode.com/problems/snapshot-array/ 题目: Implement a SnapshotArray that supports th ...

  2. 【leetcode】1146. Snapshot Array

    题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...

  3. Snapshot Array

    Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...

  4. scala位压缩与行情转换二进制

    import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer} import java.nio.charset.Charset import ...

  5. JavaScript权威指南--脚本化文档

    知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...

  6. js-NodeList对象和HTMLCollection对象

    getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...

  7. querySelector()与querySelectorAll()的区别及NodeList和HTMLCollection对象的区别

    querySelector().Document.Element类型均可调用该方法. 当用Document类型调用querySelector()方法时,会在文档元素范围内查找匹配的元素:而当用Elem ...

  8. 节点列表和HTML集合

    getElementsByName()和getElementByTagName()返回的都是NodeList集合. 而document.images和document0.forms的属性为HTMLCo ...

  9. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

随机推荐

  1. 1.1 Eclipse下载安装(+java环境)

    可直接上官网下载:http://www.eclipse.org/downloads/ 直接下载地址:http://www.eclipse.org/downloads/download.php?file ...

  2. Python学习(七)——匿名函数、map函数、filter函数、reduce函数与其他内置函数

    匿名函数 lambda x: x + 1 # lambda:定义匿名函数的关键字 # x:形参 # x+1:程序处理逻辑 fun = lambda x: x + 1 print(fun(5)) #6 ...

  3. MySQL数学函数简明总结

    1. ABS(x): 返回x的绝对值 mysql> select ABS(1), ABS(-1), ABS(0); +--------+---------+--------+| ABS(1) | ...

  4. apache的下载

    官网http://www.apache.org/ 首页第三行左右 点a number of third party vendors 再点第一个ApacheHaus 最后来到windows的下载页面 h ...

  5. JS高级---构造函数,实例对象和原型对象,三者关系

    构造函数,实例对象和原型对象,三者关系 构造函数里面有原型(prototype)属性,即原型对象 原型对象里的constryctor构造器指向构造函数 通过构造函数,实例化,创建的就是实例对象. 实例 ...

  6. Tarjan-有向图

    (我到底是咕了多少知识点啊) 在有向图中tarjan主要用来求强连通分量并缩点 一.定义 强连通:如果两个顶点可以相互通达,则称两个顶点 强连通 强连通分量:如果有向图G的每两个顶点都 强连通,称G是 ...

  7. opencv:像素统计信息

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  8. Could not find result map com.youotech.tl_cons_credit_rating.entity.Result

    后端报错如下: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.yo ...

  9. KafKa集群安装、配置

    一.事前准备 1.kafka官网:http://kafka.apache.org/downloads. 2.选择使用版本下载. 3.kafka集群环境准备:(linux) 192.168.145.12 ...

  10. spark streaming读取kakfka数据手动维护offset

    在spark streaming读取kafka的数据中,spark streaming提供了两个接口读取kafka中的数据,分别是KafkaUtils.createDstream,KafkaUtils ...