Queue + hashset
class Logger { typedef pair<string, int> Rec; queue<Rec> q; unordered_set<string> hs; public: /** Initialize your data structure here. */ Logger() { } /** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */ bool shouldPrintMessage(int timestamp, string message) { while(!q.empty() && (timestamp - q.front().second) >= 10) { hs.erase(q.front().first); q.pop(); } if(hs.find(message) != hs.end()) return false; q.push({message, timestamp}); hs.insert(message); return true; } };
时间: 2024-10-22 06:49:05