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 ...
随机推荐
- DataGridView单元格显示密码
DataGridView单元格显示密码 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormatt ...
- Linux实现树莓派3B的国密SM9算法交叉编译——(一)环境部署、简单测试与eclipse工程项目测试
这篇文章主要介绍了交叉编译的实现,包括环境部署,并简单测试交叉编译环境是否安装成功. 一.交叉编译 在一个平台上生成另一个平台上的可执行代码.为什么要大费周折的进行交叉编译呢?一句话:不得已而为之.有 ...
- 吴裕雄 python 机器学习——半监督学习标准迭代式标记传播算法LabelPropagation模型
import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...
- Java - Test - TestNG: testng.xml 元素 class
Java - Test - TestNG: testng.xml 元素 class 1. 概述 class 相关的元素 classes class methods exclude include 2. ...
- C语言运算符优先级和结合性一览表
所谓优先级就是当一个表达式中有多个运算符时,先计算谁,后计算谁.这个其实我们在小学学算术的时候就学过,如1+4÷2. 但是C语言中的运算符已经远不止四则运算中的加减乘除了,还有其他很多运算符.当它们出 ...
- LA 2995 立方体成像(模拟)
题目链接:https://vjudge.net/problem/UVALive-2995 这道题的主要难点在于三维坐标系的建立,然后在坐标系中进行迭代更新. 注意用宏定义来简化代码. AC代码: #i ...
- UITextField的内存泄漏问题
背景: 项目中使用了Facebook的FBRetainCycleDetector框架检测内存泄漏问题. 登录VC的view中放置了一个UITextField对象. 产品的要求是当进入登录界面的时候,让 ...
- 计算机二级-C语言-对标志位的巧妙使用。对二维数组数据进行处理。对文件进行数据输入。
//函数fun的功能是:计算形参x所指数组中平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于的移至后部,平均值作为返回值,在主函数中输出平均值和后移的数据. //重难 ...
- 第八届极客大挑战 Re
0x01.Writeup-RE-CM_2 题目: 解题思路: 1.这个是经过xor的,王老师提示说用xortool,于是放进kali,装好之后执行 xortool CM_2.exe -b, 0.out ...
- GCD: 求两数最大公因数算法【欧几里得法】原理的个人理解 (80%图片讲解!)
那么,求 a,b 的最大公因数就是求最大的,能均分a,b的块!