8000 refactor 359 · KateDoan/Leetcode@99613a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99613a5

Browse files
refactor 359
1 parent 859adb3 commit 99613a5

File tree

1 file changed

+29
-23
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+29
-23
lines changed

src/main/java/com/fishercoder/solutions/_359.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.util.HashSet;
55
import java.util.Map;
66
import java.util.Set;
7-
/**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.
7+
/**
8+
* 359. Logger Rate Limiter
9+
*
10+
* 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.
811
912
Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
1013
@@ -33,33 +36,36 @@ Given a message and a timestamp (in seconds granularity), return true if the mes
3336
logger.shouldPrintMessage(11,"foo"); returns true;*/
3437
public class _359 {
3538

36-
class Logger {
39+
public static class Solution1 {
40+
class Logger {
3741

38-
private Map<String, Integer> map;
39-
private Set<String> set;
42+
private Map<String, Integer> map;
43+
private Set<String> set;
4044

41-
/**
42-
* Initialize your data structure here.
43-
*/
44-
public Logger() {
45-
map = new HashMap<String, Integer>();
46-
set = new HashSet<String>();
47-
}
45+
/**
46+
* Initialize your data structure here.
47+
*/
48+
public Logger() {
49+
map = new HashMap<String, Integer>();
50+
set = new HashSet<String>();
51+
}
4852

49-
/**
50-
* Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity.
51-
*/
52-
public boolean shouldPrintMessage(int timestamp, String message) {
53-
if (!set.contains(message)) {
54-
map.put(message, timestamp);
55-
set.add(message);
56-
return true;
57-
} else {
58-
if (timestamp - map.get(message) < 10) {
59-
return false;
60-
} else {
53+
/**
54+
* Returns true if the message should be printed in the given timestamp, otherwise returns
55+
* false. The timestamp is in seconds granularity.
56+
*/
57+
public boolean shouldPrintMessage(int timestamp, String message) {
58+
if (!set.contains(message)) {
6159
map.put(message, timestamp);
60+
set.add(message);
6261
return true;
62+
} else {
63+
if (timestamp - map.get(message) < 10) {
64+
return false;
65+
} else {
66+
map.put(message, timestamp);
67+
return true;
68+
}
6369
}
6470
}
6571
}

0 commit comments

Comments
 (0)
0