代码拉取完成,页面将自动刷新
// 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);
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。