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 STL初步学习
vector, 变长数组,倍增的思想 size() 返回元素个数 empty() 返回是否为空 clear() 清空 front()/back() push_back()/pop_back() beg ...
- Bootstrap环境安装加使用---开启Bootstrap 之旅
1.首先去Bootstrap官网https://getbootstrap.com/上下载Bootstrap 的最新版本 对应的中文页面如下: 下载自己所需要的版本(目前我自己下载的是编译的css和js ...
- STL总结 (C++)
一.一般介绍 STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库.它被容纳于C++标准程序库(C++ Standard Library ...
- Azure虚拟机网站部署 防火墙设置
唯一需要注意的是当你的网站设置的端口不是默认的80的时候,需要在防火墙那里将你新设置的端口设置为allow 先要到云的后台设置 “入站安全规则”--> 将你的网站端口设置为Allow 1.通过 ...
- PHP+Mysql防止SQL注入的方法
这篇文章介绍的内容是关于PHP+Mysql防止SQL注入的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 方法一: mysql_real_escape_string -- 转义 S ...
- zabbix监控服务部署脚本
搭建平台脚本: #!/bin/bash #zabbix监控服务部署 #脚本使用前提:yum搭建,nginx-1.12.2源码包,zabbix-3.4.4源码包,要求源码包尽量在单一目录下,最好在默认管 ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.
关于以上错误,观察是否缺少了某一项,还要注意书写的顺序 建议这一类配置文件直接复制就好了,不要自己写 <?xml version="1.0" encoding="U ...
- php.ini修改php上传文件大小限制的方法
打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...
- 获取 python linux Home目录
#! /usr/bin/python # -*- coding: utf-8 -*- import os print os.environ['HOME'] print os.path.expandva ...
- docker学习及应用
一.docker发展史 为什么要学习容器? 在openstack之后,目前互联网最火热的技术莫过于docker容器了,早在2015年,京东技术备战双11就是使用了10万+Docker,这两年docke ...