LC 981. Time Based Key-Value Store
2024-09-03 05:29:32
Create a timebased key-value store class TimeMap
, that supports two operations.
1. set(string key, string value, int timestamp)
- Stores the
key
andvalue
, along with the giventimestamp
.
2. get(string key, int timestamp)
- Returns a value such that
set(key, value, timestamp_prev)
was called previously, withtimestamp_prev <= timestamp
. - If there are multiple such values, it returns the one with the largest
timestamp_prev
. - If there are no values, it returns the empty string (
""
).
Runtime: 212 ms, faster than 55.01% of C++ online submissions for Time Based Key-Value Store.
Memory Usage: 57 MB, less than 100.00% of C++ online submissions for Time Based Key-Value Store.
class TimeMap {
private:
unordered_map<string, map<int, string>> mp;
vector<int> tvec;
public:
/** Initialize your data structure here. */
TimeMap() {} void set(string key, string value, int timestamp) {
mp[key][timestamp] = value;
} string get(string key, int timestamp) {
if(!mp.count(key)) return "";
if(mp[key].count(timestamp)) return mp[key][timestamp];
for(auto it = mp[key].rbegin(); it != mp[key].rend(); it++) {
if(it->first > timestamp) continue;
else {
return it->second;
}
}
return "";
}
};
最新文章
- PHP Date()函数详细参数
- Android获取系统时间方法的总结
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
- MySql中的事件
- linux系统启动级别
- 解决IE6下浮动层固定定位的经典方法
- mysql实例 保存查询结果到变量
- Android 签名(4)验证是否签名
- IgnoreRoute——注册路由
- SSL VPN 详解
- 谷歌 google
- python-摩尔斯电码查询器
- struts2.5新配置动态调用
- SAP进度条
- 检测flash是否安装及版本号
- 库函数strstr的实现
- shell1
- Qt QGraphicsItem要点 积累
- 机械臂运动学逆解(Analytical solution)
- jquery代码修改input的value值,而页面上input框的值没有改变的解决办法