Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
    }
};
posted on 2016-06-17 05:44  Tonix  阅读(335)  评论(0编辑  收藏  举报