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. Sql Server跨服务器操作数据

    var serversSql = "select count(*) count from sys.servers WHERE name='ITSV'"; var result = ...

  2. CSS学习(9)块盒模型应用

    1.改变宽高范围 默认情况下,width和height设置的是内容盒的宽高 页面重构师:将psd文件(设计稿)制作为静态页面 衡量设计稿尺寸的时候,往往使用的是边框盒 CSS3中 box-sizing ...

  3. Python学习之列表篇

    浮点数类型:round(x,d)可对浮点数进行四舍五入,科学计数法:aeb表示a*10^bpython大小写敏感整数类型:无范围限制,pow(x,y)表示x^y,想算多大算多大,四种表示形式:十进制, ...

  4. window下进程退出后自动重启

    设计思想:编写批处理脚本监控进程的运行状态,如果发现进程停止,则自动重启该进程.批处理脚本(jk.bat)和进程脚本(hello.bat)如下: 1.jk.bat @echo off rem 定义需监 ...

  5. ubuntu mysql新增用户并开启远程连接

    1.首先用root用户登录mysql mysql -u root -p 输入密码后登录成功 2.新建用户 use mysql; select host,user from user;(查看现有用户) ...

  6. [C/C++] _tprintf() 输出不了汉字

    在前面加一个 setlocale(LC_ALL, ""); //必须得有这行 否则不能输出中文 注意得加locale头文件 #include<stdio.h> #inc ...

  7. 一组关于{x}的积分

    \[\Large\displaystyle \int_{0}^{1}\left \{ \frac{1}{x} \right \}\mathrm{d}x~,~\int_{0}^{1}\left \{ \ ...

  8. 吴裕雄 PYTHON 人工智能——基于MASK_RCNN目标检测(5)

    import os import sys import numpy as np import tensorflow as tf import matplotlib import matplotlib. ...

  9. 用数组实现 最简 hash线性探测

    package arr; import java.util.Random; /** 模拟线性寻址式hash函数 模拟将1000大小包含50个数字的数组,存入大小为100的数组内(为了方便判断,我们将0 ...

  10. 写作环境搭建(git+github+markdown+jekyll)

    转载自: https://site.douban.com/196781/widget/notes/12161495/note/264946576/ 2013-03-04 19:33:10   --- ...