Skip to content

Commit 3997818

Browse files
committed
feat: update solutions to lc problem: No.0933
No.0933.Number of Recent Calls
1 parent 9cf3111 commit 3997818

File tree

5 files changed

+103
-36
lines changed

5 files changed

+103
-36
lines changed

solution/0900-0999/0933.Number of Recent Calls/README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ recentCounter.ping(3002); // requests = [1, <strong>100</strong>, <strong>3001<
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:队列**
54+
5355
在第 1、100、3001、3002 这四个时间点分别进行了 ping 请求, 在 3001 秒的时候, 它前面的 3000 秒指的是区间 `[1,3001]`, 所以一共是有 `1、100、3001` 三个请求, t = 3002 的前 3000 秒指的是区间 `[2,3002]`, 所以有 `100、3001、3002` 三次请求。
5456

5557
可以用队列实现。每次将 t 进入队尾,同时从队头开始依次移除小于 `t-3000` 的元素。然后返回队列的大小 `q.size()` 即可。
@@ -84,14 +86,14 @@ class RecentCounter:
8486

8587
```java
8688
class RecentCounter {
87-
private Deque<Integer> q;
89+
private Deque<Integer> q = new ArrayDeque<>();
8890

8991
public RecentCounter() {
90-
q = new LinkedList<>();
92+
9193
}
92-
94+
9395
public int ping(int t) {
94-
q.offerLast(t);
96+
q.offer(t);
9597
while (q.peekFirst() < t - 3000) {
9698
q.pollFirst();
9799
}
@@ -111,17 +113,15 @@ class RecentCounter {
111113
```cpp
112114
class RecentCounter {
113115
public:
114-
deque<int> q;
116+
queue<int> q;
115117

116118
RecentCounter() {
117119

118120
}
119-
121+
120122
int ping(int t) {
121-
q.push_back(t);
122-
while (q.front() < t - 3000) {
123-
q.pop_front();
124-
}
123+
q.push(t);
124+
while (q.front() < t - 3000) q.pop();
125125
return q.size();
126126
}
127127
};
@@ -141,15 +141,13 @@ type RecentCounter struct {
141141
}
142142
143143
func Constructor() RecentCounter {
144-
return RecentCounter{
145-
q: []int{},
146-
}
144+
return RecentCounter{[]int{}}
147145
}
148146
149147
func (this *RecentCounter) Ping(t int) int {
150148
this.q = append(this.q, t)
151149
for this.q[0] < t-3000 {
152-
this.q = this.q[1:len(this.q)]
150+
this.q = this.q[1:]
153151
}
154152
return len(this.q)
155153
}
@@ -187,6 +185,33 @@ RecentCounter.prototype.ping = function (t) {
187185
*/
188186
```
189187

188+
### **C#**
189+
190+
```cs
191+
public class RecentCounter {
192+
private Queue<int> q = new Queue<int>();
193+
194+
public RecentCounter() {
195+
196+
}
197+
198+
public int Ping(int t) {
199+
q.Enqueue(t);
200+
while (q.Peek() < t - 3000)
201+
{
202+
q.Dequeue();
203+
}
204+
return q.Count;
205+
}
206+
}
207+
208+
/**
209+
* Your RecentCounter object will be instantiated and called as such:
210+
* RecentCounter obj = new RecentCounter();
211+
* int param_1 = obj.Ping(t);
212+
*/
213+
```
214+
190215
### **...**
191216

192217
```

solution/0900-0999/0933.Number of Recent Calls/README_EN.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ class RecentCounter:
7070

7171
```java
7272
class RecentCounter {
73-
private Deque<Integer> q;
73+
private Deque<Integer> q = new ArrayDeque<>();
7474

7575
public RecentCounter() {
76-
q = new LinkedList<>();
76+
7777
}
78-
78+
7979
public int ping(int t) {
80-
q.offerLast(t);
80+
q.offer(t);
8181
while (q.peekFirst() < t - 3000) {
8282
q.pollFirst();
8383
}
@@ -97,17 +97,15 @@ class RecentCounter {
9797
```cpp
9898
class RecentCounter {
9999
public:
100-
deque<int> q;
100+
queue<int> q;
101101

102102
RecentCounter() {
103103

104104
}
105-
105+
106106
int ping(int t) {
107-
q.push_back(t);
108-
while (q.front() < t - 3000) {
109-
q.pop_front();
110-
}
107+
q.push(t);
108+
while (q.front() < t - 3000) q.pop();
111109
return q.size();
112110
}
113111
};
@@ -127,15 +125,13 @@ type RecentCounter struct {
127125
}
128126
129127
func Constructor() RecentCounter {
130-
return RecentCounter{
131-
q: []int{},
132-
}
128+
return RecentCounter{[]int{}}
133129
}
134130
135131
func (this *RecentCounter) Ping(t int) int {
136132
this.q = append(this.q, t)
137133
for this.q[0] < t-3000 {
138-
this.q = this.q[1:len(this.q)]
134+
this.q = this.q[1:]
139135
}
140136
return len(this.q)
141137
}
@@ -173,6 +169,33 @@ RecentCounter.prototype.ping = function (t) {
173169
*/
174170
```
175171

172+
### **C#**
173+
174+
```cs
175+
public class RecentCounter {
176+
private Queue<int> q = new Queue<int>();
177+
178+
public RecentCounter() {
179+
180+
}
181+
182+
public int Ping(int t) {
183+
q.Enqueue(t);
184+
while (q.Peek() < t - 3000)
185+
{
186+
q.Dequeue();
187+
}
188+
return q.Count;
189+
}
190+
}
191+
192+
/**
193+
* Your RecentCounter object will be instantiated and called as such:
194+
* RecentCounter obj = new RecentCounter();
195+
* int param_1 = obj.Ping(t);
196+
*/
197+
```
198+
176199
### **...**
177200

178201
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class RecentCounter {
2+
private Queue<int> q = new Queue<int>();
3+
4+
public RecentCounter() {
5+
6+
}
7+
8+
public int Ping(int t) {
9+
q.Enqueue(t);
10+
while (q.Peek() < t - 3000)
11+
{
12+
q.Dequeue();
13+
}
14+
return q.Count;
15+
}
16+
}
17+
18+
/**
19+
* Your RecentCounter object will be instantiated and called as such:
20+
* RecentCounter obj = new RecentCounter();
21+
* int param_1 = obj.Ping(t);
22+
*/

solution/0900-0999/0933.Number of Recent Calls/Solution.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ type RecentCounter struct {
33
}
44

55
func Constructor() RecentCounter {
6-
return RecentCounter{
7-
q: []int{},
8-
}
6+
return RecentCounter{[]int{}}
97
}
108

119
func (this *RecentCounter) Ping(t int) int {
1210
this.q = append(this.q, t)
1311
for this.q[0] < t-3000 {
14-
this.q = this.q[1:len(this.q)]
12+
this.q = this.q[1:]
1513
}
1614
return len(this.q)
1715
}

solution/0900-0999/0933.Number of Recent Calls/Solution.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class RecentCounter {
2-
3-
private Deque<Integer> q;
2+
private Deque<Integer> q = new ArrayDeque<>();
43

54
public RecentCounter() {
6-
q = new ArrayDeque<>();
5+
76
}
87

98
public int ping(int t) {
10-
q.offerLast(t);
9+
q.offer(t);
1110
while (q.peekFirst() < t - 3000) {
1211
q.pollFirst();
1312
}

0 commit comments

Comments
 (0)