标签:
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; } };
LeetCode "Logger Rate Limiter"
标签:
原文地址:http://www.cnblogs.com/tonix/p/5592683.html