classTimeMap { private: /** Intution: 使用hashmap存储<key, vector<pair<int, string>> value>, 每次查找到key对应的vector后,针对pair的第一个值使用二分查找upper_bound timestamp */ unordered_map<string, vector<pair<int, string>>> hashmap; classCmp { public: booloperator()(pair<int, string> a, pair<int, string> b){ return a.first < b.first; } }; public: /** Initialize your data structure here. */ TimeMap() { } voidset(string key, string value, int timestamp){ hashmap[key].push_back({timestamp, value}); } string get(string key, int timestamp){ auto& values = hashmap[key]; // 注意:用引用,千万不要用普通变量存储 if (values.size() == 0) return""; auto val = upper_bound(values.begin(), values.end(), make_pair(timestamp, ""), Cmp()); if (val == values.begin()) return""; return (--val)->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); */
classSolution { public: intcountTriplets(vector<int>& A){ unordered_map<int, int> hashtable; int result = 0; for (auto a: A) { for (auto b: A) { int x = a & b; if (hashtable.find(x) == hashtable.end()) { for (auto c: A) { if ((c & x) == 0) hashtable[x]++; } } result += hashtable[x]; } } return result; } };