Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.

It is possible that several messages arrive roughly at the same time.

Example:

Logger logger = new Logger();

// logging string "foo" at timestamp 1
logger.shouldPrintMessage(, "foo"); returns true; // logging string "bar" at timestamp 2
logger.shouldPrintMessage(,"bar"); returns true; // logging string "foo" at timestamp 3
logger.shouldPrintMessage(,"foo"); returns false; // logging string "bar" at timestamp 8
logger.shouldPrintMessage(,"bar"); returns false; // logging string "foo" at timestamp 10
logger.shouldPrintMessage(,"foo"); returns false; // logging string "foo" at timestamp 11
logger.shouldPrintMessage(,"foo"); returns true;

思路:用队列来记录当前的所有消息,用一个set来记录哪些消息在这个队列中。

 class Message {
public:
int timestamp;
string msg;
Message(int ts, string m) : timestamp(ts), msg(m) {}
};
class Logger {
public:
/** Initialize your data structure here. */
queue<Message> logQueue;
unordered_set<string> msgInQueue;
Logger() { } /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
If this method returns false, the message will not be printed.
The timestamp is in seconds granularity. */
bool shouldPrintMessage(int timestamp, string message) {
//remove all msgs before 10s ago
while (!logQueue.empty() && logQueue.front().timestamp + <= timestamp) {
msgInQueue.erase(logQueue.front().msg);
logQueue.pop();
}
bool notInQueue = msgInQueue.count(message) == ;
//this msg will be printed, add it to log
if (notInQueue) {
logQueue.push(Message(timestamp, message));
msgInQueue.insert(message);
}
return notInQueue;
}
}; /**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* bool param_1 = obj.shouldPrintMessage(timestamp,message);
*/

Logger Rate Limiter -- LeetCode的更多相关文章

  1. 359. Logger Rate Limiter

    /* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...

  2. [LeetCode] Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  3. LeetCode Logger Rate Limiter

    原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...

  4. LeetCode 359. Logger Rate Limiter (记录速率限制器)$

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  5. [LeetCode] 359. Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  6. LeetCode 359 Logger Rate Limiter

    Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...

  7. 【LeetCode】359. Logger Rate Limiter 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. Logger Rate Limiter 十秒限时计数器

    [抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...

  9. Logger Rate Limiter

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

随机推荐

  1. bat批处理 批量导出多个APK的AAPT信息(含python实现)

    产品APP因架构调整,将一个APK拆分成了十几个APK,这样每次打ROM前,都要一个个核对APK的AAPT信息 一个个APK去敲命令很繁琐,想到可以用BAT批处理调用AAPT命令一次将十几个APK的A ...

  2. 自动化测试(三)如何用python写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱,产生的邮箱不能重复。

    写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱,产生的邮箱不能重复.邮箱前面的长度是6-12之间,产生的邮箱必须包含大写字母.小写字母.数字和特殊字符 和上一期一样 代码中间有段比较混沌 有 ...

  3. [转]unity之LOD

    LOD技术有点类似于Mipmap技术,不同的是,LOD是对模型建立了一个模型金字塔,根据摄像机距离对象的远近,选择使用不同精度的模型. 它的好处是可以在适当的时候大量减少需要绘制的顶点数目. 它的缺点 ...

  4. 孤荷凌寒自学python第六十天在windows10上搭建本地Mongodb数据服务

     孤荷凌寒自学python第六十天在windows10上找搭建本地Mongodb数据服务 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第六天.成功在本地搭建了windows ...

  5. CSU-1986 玄学

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1986 题目 Description 阴阳师子浩君,最近从<初等数论 ...

  6. php设计模式 工厂模式和单例

    1.单例模式//让该类在外界无法造对象//让外界可以造一个对象,做一个静态方法返回对象//在类里面通过让静态变量控制返回对象只能是一个. class cat{ public $name; privat ...

  7. UVALive 5027 二分图 EK

    C - Card Game Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB    64bit IO ...

  8. GCD 开发详情

    目录 一.简介 二.dispatch Queue - 队列 三.dispatch Groups - 组 四.dispatch Semaphores - 信号量 五.dispatch Barriers ...

  9. 【bzoj2238】Mst 最小生成树+树链剖分+线段树

    题目描述 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影响,即被删掉的边在下一条询问中依然存在) 输入 第一行两 ...

  10. hdu 1564 Play a game (博弈)

    Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...