考虑线性的搜索会超时,所以用二叉搜索来解决,代码如下:

 class TimeMap:
def __init__(self):
self.ST = dict() def set(self, key: 'str', value: 'str', timestamp: 'int') -> 'None':
if key in self.ST.keys():
D = self.ST[key]#dict
D.update({timestamp:value})
self.ST.update({key:D})
else:
D = dict()
D.update({timestamp:value})
self.ST.update({key:D}) def get(self, key: 'str', timestamp: 'int') -> 'str':
if key in self.ST.keys():
D = self.ST[key]
V = self.binSearch(timestamp,D)
return V
else:
return '' def binSearch(self,target,D):
times = list(D.keys())
n = len(times)
minval = times[0]
maxval = times[-1]
if target < minval:
return '' if target == minval:
return D[times[0]] if target >= maxval:
return D[times[-1]] left = 0
right = n - 1
while left < right:
mid = (left + right) // 2
if times[mid] == target:
return D[times[mid]]
elif times[mid]<target:
left = mid + 1
elif times[mid]>target:
right = mid - 1
if left == 0:
return D[times[left]]
else:
return D[times[left-1]]

但是这种写法会超时,这应该是代码质量问题,目前没明白是啥原因。

哪位博友知道我的代码的问题,欢迎告知。

参考了一下别人的方案,看到一个线性搜索的解决方案,却可以通过。

 class TimeMap:

     def __init__(self):
"""
Initialize your data structure here.
"""
self.dic = {} def set(self, key: 'str', value: 'str', timestamp: 'int') -> 'None':
if key in self.dic:
self.dic[key].append({'v': value, 't': timestamp})
else:
self.dic[key] = [{'v': value, 't': timestamp}] def get(self, key: 'str', timestamp: 'int') -> 'str':
if key in self.dic:
for kv in reversed(self.dic[key]):
if timestamp >= kv['t']:
return kv['v']
return ""
else:
return ""

leetcode981的更多相关文章

  1. [Swift]LeetCode981. 基于时间的键值存储 | Time Based Key-Value Store

    Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, s ...

随机推荐

  1. ceph存储集群性能测试工具步骤(初稿)

    一.源码安装fio工具: #yum install libaio-devel make #wget http://brick.kernel.dk/snaps/fio-2.2.10.tar.gz #ta ...

  2. 文笔很差系列1 - 也谈谈AlphaGo

    距离AlphaGo击败李世石已经过去数月了,心中的震撼至今犹在,全刊报道此项比赛的<围棋天地>杂志我已经看了不下十遍.总也想说点自己的意见,却也不知道从哪里说起,更不知道想表达些什么. 作 ...

  3. 软件工程 week 02

    一.地址链接 1.作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2110 2.git仓库地址:https://git.coding. ...

  4. NOIP2012 Day1 T2国王游戏 洛谷P1080

    第一篇博客啊…… 由于我太弱了,还要去补不全的知识点准备参加人生第一次NOIp,所以第一篇博客就简短一点好了(偷懒就直说吧……) 洛谷P1080传送门 题意概括: 有N对数ai和bi,以及两个数a0和 ...

  5. Ubuntu16.04交叉工具链安装

    前言: 开发环境是64位的ubuntu16.04,交叉工具链是通过sudo apt-get install ....安装的,移植uboot2014.10,但是很奇怪,按照网上的介绍在start.s里面 ...

  6. cordova热更新插件的使用:cordova-hot-code-push-plugin

    1. 添加插件:cordova plugin add cordova-hot-code-push-plugin 2. 先打开cli,执行命令 npm install -g cordova-hot-co ...

  7. centos7生产环境下openssh升级

    由于生产环境ssh版本太低,导致使用安全软件扫描时提示系统处于异常不安全的状态,主要原因是ssh漏洞.推荐通过升级ssh版本修复漏洞 因为是生产环境,所以有很多问题需要注意.为了保险起见,在生产环境下 ...

  8. c#利用ApplicationContext类 同时启动双窗体的实现

    Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...

  9. MySQL Point in Time Recovery the Right Way

    In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...

  10. 第一个Unity3D脚本

    学习就该简单粗暴,看了一天Unity3d的教程加文档,尝试一个小练习,再快速写个博客加深印象. 一:首先建立一个空白工程,创建一个空GameObject,在Assets Pannel中创建一个名为Le ...