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

 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. HTML5:表格相关标记及其属性

    表格相关标记及其属性 <table>:表格,包括以下属性 属性 说明 width 宽度(有像素和百分比两种表示方法) height 高度(有像素和百分比两种表示方法) border 边框粗 ...

  2. 磁性窗体设计C#(二)

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. vscode 完全支持zeng code的写法

    一.快速编写HTML代码 1.  初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...

  4. Front-end: Using blurred backgrounds with contents unaffected.

    Purpose: Using a picture as the background of a page with blurred effect, while the content not blur ...

  5. Cortex-M3 跳转到指定bin执行

    跳转前指定sp和msp: #if defined(__GNUC__) __attribute__(( naked )) static void set_sp(unsigned long addr) { ...

  6. ie8网页时调用特定的css文件

    加上条件注释语句<!--[if IE 8]><link rel="".........................><![endif]--> ...

  7. day05 字典

    今日内容(dict) 1.基本格式 2.独有方法 3.公共 4.强制转换 1.基本格式 字典(可变类型,3.6之后是有序) 帮助用户去表示一个事物的信息(事物是有多个属性) 键值不能为集合,列表,字典 ...

  8. day03运算符 逻辑运算符

    今日内容 运算符 算术运算符 取模% 打印1~100基数 #模2余1的为基数 #以1 3 5 7 9结尾的为奇数 # count =1 # while count<100: # print(co ...

  9. 全志A33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  10. Kibana简介及下载安装

    现在你已经安装了Kibana,现在你一步步通过本教程快速获取Kibana核心功能的实践经验.学习完本教程,你将: 1.加载案例数据到你安装的Elasticsearch中 2. 定义至少一个索引匹配模式 ...