Solution URL
https://leetcode.com/submissions/detail/415238637/
代码
class Logger {
//ans: Solution
//use hashMap to store msg and timestamp
HashMap<String, Integer> msg;
/** Initialize your data structure here. */
public Logger() {
msg = new HashMap<>();
}
/** 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 msg does not contain the message, store it and return true;
if(!msg.containsKey(message)){
msg.put(message, timestamp);
return true;
}
//if msg contains the message
//if the time interval is more than 10 sec
int interval = timestamp - msg.get(message);
if(interval >= 10){
msg.put(message, timestamp);
return true;
}else{
//if the interval is less than 10 sec
return false;
}
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/