Re: [闲聊] 每日LeetCode

楼主: fxfxxxfxx (爱丽丝)   2022-10-06 15:02:54
981. Time Based Key-Value Store
这几天的每日一题都比较直观(无聊)
class TimeMap {
public:
unordered_map<string, vector<pair<int,string>>> M;
TimeMap() {}
void set(string key, string value, int timestamp) {
M[key].push_back({timestamp, value});
}
string get(string key, int timestamp) {
const pair<int, string> pivot{timestamp, "fxfxxxfxx"};
constexpr auto cmp = [](decltype(pivot) a, decltype(pivot) b) {
return a.first > b.first;
};
auto it = lower_bound(M[key].rbegin(), M[key].rend(), pivot, cmp);
return (it == M[key].rend()) ? "" : it->second;
}
};
有点丑,不过随便喇
倒著用std::lower_bound()
作者: Rushia (みけねこ的鼻屎)   2022-10-06 15:03:00
好好喔 都有酷酷的函式库
作者: MikuLover (爱ミク的人)   2022-10-06 15:05:00
跨谋啦
作者: pandix (面包屌)   2022-10-06 15:12:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com