LeetCode 359 Logger Rate Limiter
Problem:
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.
Summary:
设计一个日志系统,判断信息流中的信息能否在特定时间发送。每次传入一个字符串以及发送时间,若该字符串在前十秒未发送过,则return true,否则return false,不发送该字符串。
Solution:
1. unordered_map中存入上次某字符串的发送时间。
class Logger {
public:
Logger() {
}
bool shouldPrintMessage(int timestamp, string message) {
if (!m.count(message) || timestamp - m[message] >= ) {
m[message] = timestamp;
return true;
}
else {
return false;
}
}
private:
unordered_map<string, int> m;
};
2. 简便写法:unordered_map存入某字符串下次可发送时间。
class Logger {
public:
Logger() {
}
bool shouldPrintMessage(int timestamp, string message) {
if (m[message] > timestamp) {
return false;
}
m[message] = timestamp + ;
return true;
}
private:
unordered_map<string, int> m;
};
LeetCode 359 Logger Rate Limiter的更多相关文章
- LeetCode 359. Logger Rate Limiter (记录速率限制器)$
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] 359. Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...
- 【LeetCode】359. Logger Rate Limiter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LC] 359. Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- LeetCode Logger Rate Limiter
原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...
- Logger Rate Limiter -- LeetCode
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- Logger Rate Limiter 十秒限时计数器
[抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...
随机推荐
- ThinkPHP 多语言
1.注意区分项目语言包和系统语言包 2.实现语言包和数据库语言同步切换 实用链接: ThinkPHP完全开发手册3.1 多语言 thinkphp3.1 多语言简单demo 总结ThinkPHP使用技巧 ...
- C# 在数组中判断是否存在某个数组值
(1) 第一种方法: ,,}; ); // 这里的1就是你要查找的值 ) // 不存在 else // 存在 (2) 第二种方法: string[] strArr = {"a",& ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- MYSQL的大数据量情况下的分页查询优化
最近做的项目需要实现一个分页查询功能,自己先看了别人写的方法: <!-- 查询 --> <select id="queryMonitorFolder" param ...
- 使用winmm.dll 获取麦克风声音数据
//录音 /// <summary> /// 初始化录音环境 /// </summary> /// <returns></returns> public ...
- Matlab中^2和.^2的区别
矩阵a a^2 -- 两个矩阵相乘 a.^2 -- 表示 矩阵对应位置相乘 如下: a=[ 1,2,3 4,5,6 7,8,9]; disp(a); disp(a^2); disp(a.^2); ...
- angualr 实现tab选项卡功能
tab.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Linux下的几个好用的命令与参数
将所有文件的编码,转换为UTF-8 find . ! -type d -exec enca -L zh_CN -x UTF-8 {} \; 将指定目录下所有文件权限设定为644 find . ! -t ...
- POJ2096 Collecting Bugs
Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 5090 Accepted: 2529 Case Time Limit: ...
- favicon.ico 404的问题(title栏前面的图标)
1.页面中自定义图标 去 http://www.bitbug.net/ 定制图片,有32*32,16*16等样式可供选择 2.在页面中引入定义的图片 <link rel="sho ...