LeetCode Logger Rate Limiter
原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/
题目:
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(1, "foo"); returns true; // logging string "bar" at timestamp 2
logger.shouldPrintMessage(2,"bar"); returns true; // logging string "foo" at timestamp 3
logger.shouldPrintMessage(3,"foo"); returns false; // logging string "bar" at timestamp 8
logger.shouldPrintMessage(8,"bar"); returns false; // logging string "foo" at timestamp 10
logger.shouldPrintMessage(10,"foo"); returns false; // logging string "foo" at timestamp 11
logger.shouldPrintMessage(11,"foo"); returns true;
题解:
再次看到这题就像回到了当年的战场.
维护一个HashMap, key 是message, value 是该message的timestamp
没出现过的message加到HashMap中, return true.
出现过并没有超过10的message return false.
出现过并超过10的message, 跟新HashMap, return true.
Time Complexity: shouldPrintMessage O(1).
Space: HashMap的大小.
AC Java:
public class Logger { /** Initialize your data structure here. */
HashMap<String, Integer> hm;
public Logger() {
hm = new HashMap<String, Integer>();
} /** 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. */
public boolean shouldPrintMessage(int timestamp, String message) {
if(!hm.containsKey(message)){
hm.put(message, timestamp);
return true;
}else if(timestamp - hm.get(message) < 10){
return false;
}else{
hm.put(message, timestamp);
return true;
}
}
} /**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
LeetCode Logger Rate Limiter的更多相关文章
- [LeetCode] 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 (记录速率限制器)$
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 ...
- LeetCode 359 Logger Rate Limiter
Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...
- Logger Rate Limiter -- LeetCode
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- 【LeetCode】359. Logger Rate Limiter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- Logger Rate Limiter 十秒限时计数器
[抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...
- Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
随机推荐
- 使用MultipartEntity进行post请求的默认MIME类型
MultipartEntity .FileBody的默认MIME类型:application/octet-stream
- android 第三方登录 --- QQ
1,jar包 2,配置AndroidManifest.xml <activity android:name=".login.LoginActivity" android:co ...
- Power BI FAQ
关于Power BI,最近一直想写点什么,但是也想不到写什么.直到前几天,有个朋友在qq加我,问了我好几个问题,我发现都是一些很有代表性的问题,所以都记录了下来,特意整理了一下分享出来,希望能帮到更多 ...
- HDU 2509 Be the Winner nim博弈变形
Be the Winner Problem Description Let's consider m apples divided into n groups. Each group contai ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- 对JavaScript中异步同步机制以及线程深入了解
今天在网上看到各种对Js异步同步单线程多线程的讨论 经过前辈们的洗礼 加上鄙人小小的理解 就来纸上谈兵一下吧~ Js本身就是单线程的 至于为什么Js是单线程的 那就要追溯到Js的历史了 总而言之 由于 ...
- JAVA Day11
接口 interface public interface Demo { public abstract void demo(); } 接口中的方法全部都是抽象的. 接口的特性 接口不可以被实例化 ...
- hdu分类 Math Theory(还有三题!)
这个分类怎么觉得这么水呢.. 这个分类做到尾的模板集: //gcd int gcd(int a,int b){return b? gcd(b, a % b) : a;} //埃氏筛法 O(nlogn) ...
- react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)
react学习小结 本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...
- 创建DOTA2本地数据库(一)
在APP中,用本地数据库好于频繁的联网去获取相关数据.我使用SQLite作为本地的数据库,比较轻巧. 英雄 首先先建立英雄的数据库,暂时我先只设置ID,name,loaclized_name这三种,我 ...