LC 981. Time Based Key-Value Store

Posted ethanhong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LC 981. Time Based Key-Value Store相关的知识,希望对你有一定的参考价值。

Create a timebased key-value store class TimeMap, that supports two operations.

1. set(string key, string value, int timestamp)

  • Stores the key and value, along with the given timestamp.

2. get(string key, int timestamp)

  • Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_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 "";
  }
};

 

以上是关于LC 981. Time Based Key-Value Store的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 981. Time Based Key-Value Store

[MSTL] lc981. 基于时间的键值存储(设计+哈希表+map二分查找+代码技巧)

启动 vim 时出现错误消息:“无法将区域设置类别 LC_NUMERIC 设置为 en_CH”(或 en_BR、en_RU 和 LC_TIME、LC_COLLATE、LC_MONETARY、LC_ME

将Eclipse Maven项目 导入 IDEA 步骤 成功运行 已测试!~LC

记一次python沙箱逃逸绕过(time based rce)

Edge Computing Application: Real-Time Face Recognition Based on Cloudlet