File tree Expand file tree Collapse file tree 4 files changed +188
-0
lines changed
solution/0900-0999/0933.Number of Recent Calls Expand file tree Collapse file tree 4 files changed +188
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,72 @@ public class RecentCounter {
212
212
*/
213
213
```
214
214
215
+ ### ** TypeScript**
216
+
217
+ ``` ts
218
+ class RecentCounter {
219
+ private queue: number [];
220
+
221
+ constructor () {
222
+ this .queue = [];
223
+ }
224
+
225
+ ping(t : number ): number {
226
+ this .queue .push (t );
227
+ while (this .queue [0 ] < t - 3000 ) {
228
+ this .queue .shift ();
229
+ }
230
+ return this .queue .length ;
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Your RecentCounter object will be instantiated and called as such:
236
+ * var obj = new RecentCounter()
237
+ * var param_1 = obj.ping(t)
238
+ */
239
+ ```
240
+
241
+ ### ** Rust**
242
+
243
+ ``` rust
244
+ use std :: collections :: VecDeque ;
245
+ struct RecentCounter {
246
+ queue : VecDeque <i32 >
247
+ }
248
+
249
+
250
+ /**
251
+ * `&self` means the method takes an immutable reference.
252
+ * If you need a mutable reference, change it to `&mut self` instead.
253
+ */
254
+ impl RecentCounter {
255
+
256
+ fn new () -> Self {
257
+ Self {
258
+ queue : VecDeque :: new ()
259
+ }
260
+ }
261
+
262
+ fn ping (& mut self , t : i32 ) -> i32 {
263
+ self . queue. push_back (t );
264
+ while let Some (& v ) = self . queue. front () {
265
+ if v >= t - 3000 {
266
+ break ;
267
+ }
268
+ self . queue. pop_front ();
269
+ }
270
+ self . queue. len () as i32
271
+ }
272
+ }
273
+
274
+ /**
275
+ * Your RecentCounter object will be instantiated and called as such:
276
+ * let obj = RecentCounter::new();
277
+ * let ret_1: i32 = obj.ping(t);
278
+ */
279
+ ```
280
+
215
281
### ** ...**
216
282
217
283
```
Original file line number Diff line number Diff line change @@ -196,6 +196,72 @@ public class RecentCounter {
196
196
*/
197
197
```
198
198
199
+ ### ** TypeScript**
200
+
201
+ ``` ts
202
+ class RecentCounter {
203
+ private queue: number [];
204
+
205
+ constructor () {
206
+ this .queue = [];
207
+ }
208
+
209
+ ping(t : number ): number {
210
+ this .queue .push (t );
211
+ while (this .queue [0 ] < t - 3000 ) {
212
+ this .queue .shift ();
213
+ }
214
+ return this .queue .length ;
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Your RecentCounter object will be instantiated and called as such:
220
+ * var obj = new RecentCounter()
221
+ * var param_1 = obj.ping(t)
222
+ */
223
+ ```
224
+
225
+ ### ** Rust**
226
+
227
+ ``` rust
228
+ use std :: collections :: VecDeque ;
229
+ struct RecentCounter {
230
+ queue : VecDeque <i32 >
231
+ }
232
+
233
+
234
+ /**
235
+ * `&self` means the method takes an immutable reference.
236
+ * If you need a mutable reference, change it to `&mut self` instead.
237
+ */
238
+ impl RecentCounter {
239
+
240
+ fn new () -> Self {
241
+ Self {
242
+ queue : VecDeque :: new ()
243
+ }
244
+ }
245
+
246
+ fn ping (& mut self , t : i32 ) -> i32 {
247
+ self . queue. push_back (t );
248
+ while let Some (& v ) = self . queue. front () {
249
+ if v >= t - 3000 {
250
+ break ;
251
+ }
252
+ self . queue. pop_front ();
253
+ }
254
+ self . queue. len () as i32
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Your RecentCounter object will be instantiated and called as such:
260
+ * let obj = RecentCounter::new();
261
+ * let ret_1: i32 = obj.ping(t);
262
+ */
263
+ ```
264
+
199
265
### ** ...**
200
266
201
267
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: VecDeque ;
2
+ struct RecentCounter {
3
+ queue : VecDeque < i32 >
4
+ }
5
+
6
+
7
+ /**
8
+ * `&self` means the method takes an immutable reference.
9
+ * If you need a mutable reference, change it to `&mut self` instead.
10
+ */
11
+ impl RecentCounter {
12
+
13
+ fn new ( ) -> Self {
14
+ Self {
15
+ queue : VecDeque :: new ( )
16
+ }
17
+ }
18
+
19
+ fn ping ( & mut self , t : i32 ) -> i32 {
20
+ self . queue . push_back ( t) ;
21
+ while let Some ( & v) = self . queue . front ( ) {
22
+ if v >= t - 3000 {
23
+ break ;
24
+ }
25
+ self . queue . pop_front ( ) ;
26
+ }
27
+ self . queue . len ( ) as i32
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Your RecentCounter object will be instantiated and called as such:
33
+ * let obj = RecentCounter::new();
34
+ * let ret_1: i32 = obj.ping(t);
35
+ */
Original file line number Diff line number Diff line change
1
+ class RecentCounter {
2
+ private queue : number [ ] ;
3
+
4
+ constructor ( ) {
5
+ this . queue = [ ] ;
6
+ }
7
+
8
+ ping ( t : number ) : number {
9
+ this . queue . push ( t ) ;
10
+ while ( this . queue [ 0 ] < t - 3000 ) {
11
+ this . queue . shift ( ) ;
12
+ }
13
+ return this . queue . length ;
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Your RecentCounter object will be instantiated and called as such:
19
+ * var obj = new RecentCounter()
20
+ * var param_1 = obj.ping(t)
21
+ */
You can’t perform that action at this time.
0 commit comments