Skip to content

Commit 0cf738f

Browse files
authored
feat: add swift implementation to lcof2 problem: No.042 (#3052)
1 parent 00b7e53 commit 0cf738f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lcof2/剑指 Offer II 042. 最近请求次数/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,32 @@ RecentCounter.prototype.ping = function (t) {
213213
*/
214214
```
215215

216+
#### Swift
217+
218+
```swift
219+
class RecentCounter {
220+
private var q: [Int]
221+
222+
init() {
223+
q = []
224+
}
225+
226+
func ping(_ t: Int) -> Int {
227+
q.append(t)
228+
while q.first! < t - 3000 {
229+
q.removeFirst()
230+
}
231+
return q.count
232+
}
233+
}
234+
235+
/**
236+
* Your RecentCounter object will be instantiated and called as such:
237+
* let obj = RecentCounter()
238+
* let param_1 = obj.ping(t)
239+
*/
240+
```
241+
216242
<!-- tabs:end -->
217243

218244
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class RecentCounter {
2+
private var q: [Int]
3+
4+
init() {
5+
q = []
6+
}
7+
8+
func ping(_ t: Int) -> Int {
9+
q.append(t)
10+
while q.first! < t - 3000 {
11+
q.removeFirst()
12+
}
13+
return q.count
14+
}
15+
}
16+
17+
/**
18+
* Your RecentCounter object will be instantiated and called as such:
19+
* let obj = RecentCounter()
20+
* let param_1 = obj.ping(t)
21+
*/

0 commit comments

Comments
 (0)