Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcbba375

Browse files
logger rate limiter
1 parent83fde9e commitcbba375

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

‎EASY/src/easy/LoggerRateLimiter.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packageeasy;
2+
3+
importjava.util.HashMap;
4+
importjava.util.HashSet;
5+
importjava.util.Map;
6+
importjava.util.Set;
7+
8+
publicclassLoggerRateLimiter {
9+
10+
}
11+
12+
classLogger {
13+
14+
privateMap<String,Integer>map;
15+
privateSet<String>set;
16+
17+
/** Initialize your data structure here. */
18+
publicLogger() {
19+
map =newHashMap<String,Integer>();
20+
set =newHashSet<String>();
21+
}
22+
23+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
24+
publicbooleanshouldPrintMessage(inttimestamp,Stringmessage) {
25+
if(!set.contains(message)) {
26+
map.put(message,timestamp);
27+
set.add(message);
28+
returntrue;
29+
}else {
30+
if(timestamp -map.get(message) <10)returnfalse;
31+
else{
32+
map.put(message,timestamp);
33+
returntrue;
34+
}
35+
}
36+
}
37+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../../blob/master/MEDIUM/src/medium/DesignPhoneDirectory.java)| O(1)|O(n)| Medium|
3333
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
3434
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../../blob/master/MEDIUM/src/medium/RangeAddition.java)| O(n+k)|O(1)| Medium|
35+
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../../blob/master/EASY/src/easy/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
3536
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3637
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
3738
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../../blob/master/EASY/src/easy/MovingAveragefromDataStream.java)| O(1)|O(w)) | Easy| Queue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp