码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Logger Rate Limiter"

时间:2016-06-17 06:16:59      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!