@@ -46,21 +46,103 @@ logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, nex
46
46
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>shouldPrintMessage</code>.</li>
47
47
</ul >
48
48
49
-
50
49
## Solutions
51
50
52
51
<!-- tabs:start -->
53
52
54
53
### ** Python3**
55
54
56
55
``` python
57
-
56
+ class Logger :
57
+
58
+ def __init__ (self ):
59
+ """
60
+ Initialize your data structure here.
61
+ """
62
+ self .limiter = {}
63
+
64
+ def shouldPrintMessage (self , timestamp : int , message : str ) -> bool :
65
+ """
66
+ Returns true if the message should be printed in the given timestamp, otherwise returns false.
67
+ If this method returns false, the message will not be printed.
68
+ The timestamp is in seconds granularity.
69
+ """
70
+ t = self .limiter.get(message, 0 )
71
+ if t > timestamp:
72
+ return False
73
+ self .limiter[message] = timestamp + 10
74
+ return True
75
+
76
+
77
+ # Your Logger object will be instantiated and called as such:
78
+ # obj = Logger()
79
+ # param_1 = obj.shouldPrintMessage(timestamp,message)
58
80
```
59
81
60
82
### ** Java**
61
83
62
84
``` java
85
+ class Logger {
86
+
87
+ private Map<String , Integer > limiter;
88
+
89
+ /* * Initialize your data structure here. */
90
+ public Logger () {
91
+ limiter = new HashMap<> ();
92
+ }
93
+
94
+ /* * Returns true if the message should be printed in the given timestamp, otherwise returns false.
95
+ If this method returns false, the message will not be printed.
96
+ The timestamp is in seconds granularity. */
97
+ public boolean shouldPrintMessage (int timestamp , String message ) {
98
+ int t = limiter. getOrDefault(message, 0 );
99
+ if (t > timestamp) {
100
+ return false ;
101
+ }
102
+ limiter. put(message, timestamp + 10 );
103
+ return true ;
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Your Logger object will be instantiated and called as such:
109
+ * Logger obj = new Logger();
110
+ * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
111
+ */
112
+ ```
63
113
114
+ ### ** JavaScript**
115
+
116
+ ``` js
117
+ /**
118
+ * Initialize your data structure here.
119
+ */
120
+ var Logger = function () {
121
+ this .limiter = {};
122
+ };
123
+
124
+ /**
125
+ * Returns true if the message should be printed in the given timestamp, otherwise returns false.
126
+ If this method returns false, the message will not be printed.
127
+ The timestamp is in seconds granularity.
128
+ * @param {number} timestamp
129
+ * @param {string} message
130
+ * @return {boolean}
131
+ */
132
+ Logger .prototype .shouldPrintMessage = function (timestamp , message ) {
133
+ const t = this .limiter [message] || 0 ;
134
+ if (t > timestamp) {
135
+ return false ;
136
+ }
137
+ this .limiter [message] = timestamp + 10 ;
138
+ return true ;
139
+ };
140
+
141
+ /**
142
+ * Your Logger object will be instantiated and called as such:
143
+ * var obj = new Logger()
144
+ * var param_1 = obj.shouldPrintMessage(timestamp,message)
145
+ */
64
146
```
65
147
66
148
### ** ...**
0 commit comments