Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
time-based-key-value-store.cpp 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-27 16:41 +08:00 . Create time-based-key-value-store.cpp
// Time: set: O(1)
// get: O(logn)
// Space: O(n)
class TimeMap {
public:
/** Initialize your data structure here. */
TimeMap() {
}
void set(string key, string value, int timestamp) {
lookup_[key].emplace_back(timestamp, value);
}
string get(string key, int timestamp) {
if (!lookup_.count(key)) {
return "";
}
auto it = upper_bound(lookup_[key].cbegin(),
lookup_[key].cend(),
make_pair(timestamp + 1, ""),
[](const pair<int, string>& lhs,
const pair<int, string>& rhs) {
return lhs.first < rhs.first;
});
return it != lookup_[key].cbegin() ? prev(it)->second : "";
}
private:
unordered_map<string, vector<pair<int, string>>> lookup_;
};
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap* obj = new TimeMap();
* obj->set(key,value,timestamp);
* string param_2 = obj->get(key,timestamp);
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助