forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggerRateLimiter.java
37 lines (30 loc) · 957 Bytes
/
LoggerRateLimiter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package easy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class LoggerRateLimiter {
}
class Logger {
private Map<String, Integer> map;
private Set<String> set;
/** Initialize your data structure here. */
public Logger() {
map = new HashMap<String, Integer>();
set = new HashSet<String>();
}
/** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
public boolean shouldPrintMessage(int timestamp, String message) {
if(!set.contains(message)) {
map.put(message, timestamp);
set.add(message);
return true;
} else {
if(timestamp - map.get(message) < 10) return false;
else{
map.put(message, timestamp);
return true;
}
}
}
}