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 givenindexto 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
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的更多相关文章
- LeetCode 1146. Snapshot Array
原题链接在这里:https://leetcode.com/problems/snapshot-array/ 题目: Implement a SnapshotArray that supports th ...
- 【leetcode】1146. Snapshot Array
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...
- Snapshot Array
Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...
- scala位压缩与行情转换二进制
import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer} import java.nio.charset.Charset import ...
- JavaScript权威指南--脚本化文档
知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...
- js-NodeList对象和HTMLCollection对象
getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...
- querySelector()与querySelectorAll()的区别及NodeList和HTMLCollection对象的区别
querySelector().Document.Element类型均可调用该方法. 当用Document类型调用querySelector()方法时,会在文档元素范围内查找匹配的元素:而当用Elem ...
- 节点列表和HTML集合
getElementsByName()和getElementByTagName()返回的都是NodeList集合. 而document.images和document0.forms的属性为HTMLCo ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
随机推荐
- AcWing 872. 最大公约数
#include <iostream> #include <algorithm> using namespace std; //辗转相除法 //a和b的最大公约数 = b和(a ...
- Acwing 蛇形矩阵
Acwing 蛇形矩阵 package javaqq; import java.util.Scanner; public class 蛇形 { public static void main(Stri ...
- 寒假安卓app开发学习记录(4)
今天的任务第一个项目运行并部署.没想到第一个环节就出现了错误,运行之后eclipse报错:Errors occurred during the build.Errors running builder ...
- 502,csssprite是什么,有什么优缺点
(百科:csssprite是一种网页图片应用处理方式,国内常叫css精灵.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢慢 ...
- argparse 模块使用
import argparse,os data_func=["upload","download"]req_func=["getfunc", ...
- XMOS发布集单片机,AI,FPGA,DSP于一身的跨界处理器完全体xcore.ai,致力于AIOT,售价1美元起步
说明:XMOS这次致力于打造全新的,颠覆性的嵌入式平台,简化开发人员要学一堆东西才能开发一款高性能AIOT产品的痛点. XCORE.AI集单片机,AI,FPGA,DSP于一身,嵌入式软件开发人员可以灵 ...
- java并发框架--Fork-Join
并行计算 Fork-Join 关键类 例子 package sumTest2; /** * 计算1-10000000的和 * 适用范围:计算不知道计算量大小的计算 */ import java.uti ...
- Web基础了解版07-EL表达式-运算符-11个隐式对象
EL EL(Expression Language)是JSP内置的表达式语言,用以访问页面的上下文以及不同作用域中的对象 ,取得对象属性的值,或执行简单的运算或判断操作.EL在得到某个数据时,会自动进 ...
- 【转载】 BIOS设置选项详细解释——CPU核心篇
原文地址: http://kuaibao.qq.com/s/20180226A1G1OC00?refer=spider ---------------------------------------- ...
- zookeeper基本使用
(1)查看节点信息:ls / (2)查看单个节点的状态:stat /zookeeper (3)在Java中使用的zk客户端:zkClient,curator (4)curator是apache的开源的 ...