首页 > 其他 > 详细

LC981-基于时间的键值存储

时间:2021-07-10 22:14:51      阅读:34      评论:0      收藏:0      [点我收藏+]

981. 基于时间的键值存储

二分

class TimeMap {
public:
    unordered_map<string,vector<pair<int,string>>>hash;
    /** Initialize your data structure here. */
    TimeMap() {}
    
    void set(string key, string value, int timestamp) {
        hash[key].push_back({timestamp,value});
    }
    
    string get(string key, int timestamp) {
        if(!hash.count(key))return "";
        auto& q = hash[key];
        int l = 0, r = q.size() - 1;
        while(l < r){
            int mid = l + (r - l + 1) / 2;
            if(q[mid].first <= timestamp)l = mid;
            else r = mid - 1;
        }
        if(q[r].first > timestamp)return "";
        return q[l].second;
    }
};

/**
 * 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);
 */

LC981-基于时间的键值存储

原文:https://www.cnblogs.com/Ivessas/p/14994917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!