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

Commit 2828902

Browse files
committed
feat: add solutions to leetcode problem: No.0362. Design Hit Counter
1 parent d073a92 commit 2828902

File tree

4 files changed

+188
-6
lines changed

4 files changed

+188
-6
lines changed

‎solution/0300-0399/0362.Design Hit Counter/README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,88 @@ counter.getHits(301);
4242

4343
<p>如果每秒的敲击次数是一个很大的数字,你的计数器可以应对吗?</p>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->
4948

49+
用哈希表作为计数器实现。
50+
5051
<!-- tabs:start -->
5152

5253
### **Python3**
5354

5455
<!-- 这里可写当前语言的特殊实现逻辑 -->
5556

5657
```python
57-
58+
class HitCounter:
59+
60+
def __init__(self):
61+
"""
62+
Initialize your data structure here.
63+
"""
64+
self.counter = collections.Counter()
65+
66+
def hit(self, timestamp: int) -> None:
67+
"""
68+
Record a hit.
69+
@param timestamp - The current timestamp (in seconds granularity).
70+
"""
71+
self.counter[timestamp] += 1
72+
73+
74+
def getHits(self, timestamp: int) -> int:
75+
"""
76+
Return the number of hits in the past 5 minutes.
77+
@param timestamp - The current timestamp (in seconds granularity).
78+
"""
79+
return sum([v for t, v in self.counter.items() if t + 300 > timestamp])
80+
81+
82+
# Your HitCounter object will be instantiated and called as such:
83+
# obj = HitCounter()
84+
# obj.hit(timestamp)
85+
# param_2 = obj.getHits(timestamp)
5886
```
5987

6088
### **Java**
6189

6290
<!-- 这里可写当前语言的特殊实现逻辑 -->
6391

6492
```java
65-
93+
class HitCounter {
94+
95+
private Map<Integer, Integer> counter;
96+
97+
/** Initialize your data structure here. */
98+
public HitCounter() {
99+
counter = new HashMap<>();
100+
}
101+
102+
/** Record a hit.
103+
@param timestamp - The current timestamp (in seconds granularity). */
104+
public void hit(int timestamp) {
105+
counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
106+
}
107+
108+
/** Return the number of hits in the past 5 minutes.
109+
@param timestamp - The current timestamp (in seconds granularity). */
110+
public int getHits(int timestamp) {
111+
int hits = 0;
112+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
113+
if (entry.getKey() + 300 > timestamp) {
114+
hits += entry.getValue();
115+
}
116+
}
117+
return hits;
118+
}
119+
}
120+
121+
/**
122+
* Your HitCounter object will be instantiated and called as such:
123+
* HitCounter obj = new HitCounter();
124+
* obj.hit(timestamp);
125+
* int param_2 = obj.getHits(timestamp);
126+
*/
66127
```
67128

68129
### **...**

‎solution/0300-0399/0362.Design Hit Counter/README_EN.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,80 @@ hitCounter.getHits(301); // get hits at timestamp 301, return 3.
4949
<p>&nbsp;</p>
5050
<p><strong>Follow up:</strong> What if the number of hits per second could be huge? Does your design scale?</p>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```python
60-
59+
class HitCounter:
60+
61+
def __init__(self):
62+
"""
63+
Initialize your data structure here.
64+
"""
65+
self.counter = collections.Counter()
66+
67+
def hit(self, timestamp: int) -> None:
68+
"""
69+
Record a hit.
70+
@param timestamp - The current timestamp (in seconds granularity).
71+
"""
72+
self.counter[timestamp] += 1
73+
74+
75+
def getHits(self, timestamp: int) -> int:
76+
"""
77+
Return the number of hits in the past 5 minutes.
78+
@param timestamp - The current timestamp (in seconds granularity).
79+
"""
80+
return sum([v for t, v in self.counter.items() if t + 300 > timestamp])
81+
82+
83+
# Your HitCounter object will be instantiated and called as such:
84+
# obj = HitCounter()
85+
# obj.hit(timestamp)
86+
# param_2 = obj.getHits(timestamp)
6187
```
6288

6389
### **Java**
6490

6591
```java
66-
92+
class HitCounter {
93+
94+
private Map<Integer, Integer> counter;
95+
96+
/** Initialize your data structure here. */
97+
public HitCounter() {
98+
counter = new HashMap<>();
99+
}
100+
101+
/** Record a hit.
102+
@param timestamp - The current timestamp (in seconds granularity). */
103+
public void hit(int timestamp) {
104+
counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
105+
}
106+
107+
/** Return the number of hits in the past 5 minutes.
108+
@param timestamp - The current timestamp (in seconds granularity). */
109+
public int getHits(int timestamp) {
110+
int hits = 0;
111+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
112+
if (entry.getKey() + 300 > timestamp) {
113+
hits += entry.getValue();
114+
}
115+
}
116+
return hits;
117+
}
118+
}
119+
120+
/**
121+
* Your HitCounter object will be instantiated and called as such:
122+
* HitCounter obj = new HitCounter();
123+
* obj.hit(timestamp);
124+
* int param_2 = obj.getHits(timestamp);
125+
*/
67126
```
68127

69128
### **...**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class HitCounter {
2+
3+
private Map<Integer, Integer> counter;
4+
5+
/** Initialize your data structure here. */
6+
public HitCounter() {
7+
counter = new HashMap<>();
8+
}
9+
10+
/** Record a hit.
11+
@param timestamp - The current timestamp (in seconds granularity). */
12+
public void hit(int timestamp) {
13+
counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
14+
}
15+
16+
/** Return the number of hits in the past 5 minutes.
17+
@param timestamp - The current timestamp (in seconds granularity). */
18+
public int getHits(int timestamp) {
19+
int hits = 0;
20+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
21+
if (entry.getKey() + 300 > timestamp) {
22+
hits += entry.getValue();
23+
}
24+
}
25+
return hits;
26+
}
27+
}
28+
29+
/**
30+
* Your HitCounter object will be instantiated and called as such:
31+
* HitCounter obj = new HitCounter();
32+
* obj.hit(timestamp);
33+
* int param_2 = obj.getHits(timestamp);
34+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class HitCounter:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.counter = collections.Counter()
8+
9+
def hit(self, timestamp: int) -> None:
10+
"""
11+
Record a hit.
12+
@param timestamp - The current timestamp (in seconds granularity).
13+
"""
14+
self.counter[timestamp] += 1
15+
16+
17+
def getHits(self, timestamp: int) -> int:
18+
"""
19+
Return the number of hits in the past 5 minutes.
20+
@param timestamp - The current timestamp (in seconds granularity).
21+
"""
22+
return sum([v for t, v in self.counter.items() if t + 300 > timestamp])
23+
24+
25+
# Your HitCounter object will be instantiated and called as such:
26+
# obj = HitCounter()
27+
# obj.hit(timestamp)
28+
# param_2 = obj.getHits(timestamp)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /