Skip to content

Commit 5fe3866

Browse files
authored
feat: add swift implementation to lcof problem: No.62 (#2954)
1 parent 6292326 commit 5fe3866

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lcof/面试题62. 圆圈中最后剩下的数字/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ public class Solution {
161161
}
162162
```
163163

164+
#### Swift
165+
166+
```swift
167+
class Solution {
168+
func lastRemaining(_ n: Int, _ m: Int) -> Int {
169+
return f(n, m)
170+
}
171+
172+
private func f(_ n: Int, _ m: Int) -> Int {
173+
if n == 1 {
174+
return 0
175+
}
176+
let x = f(n - 1, m)
177+
return (m + x) % n
178+
}
179+
}
180+
```
181+
164182
<!-- tabs:end -->
165183

166184
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func lastRemaining(_ n: Int, _ m: Int) -> Int {
3+
return f(n, m)
4+
}
5+
6+
private func f(_ n: Int, _ m: Int) -> Int {
7+
if n == 1 {
8+
return 0
9+
}
10+
let x = f(n - 1, m)
11+
return (m + x) % n
12+
}
13+
}

0 commit comments

Comments
 (0)