feat: add swift implementation to lcof problem: No.62 (#2954)

This commit is contained in:
Lanre Adedara 2024-05-29 09:36:55 +01:00 committed by GitHub
parent 62923268ff
commit 5fe38660fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 0 deletions

View File

@ -161,6 +161,24 @@ public class Solution {
}
```
#### Swift
```swift
class Solution {
func lastRemaining(_ n: Int, _ m: Int) -> Int {
return f(n, m)
}
private func f(_ n: Int, _ m: Int) -> Int {
if n == 1 {
return 0
}
let x = f(n - 1, m)
return (m + x) % n
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,13 @@
class Solution {
func lastRemaining(_ n: Int, _ m: Int) -> Int {
return f(n, m)
}
private func f(_ n: Int, _ m: Int) -> Int {
if n == 1 {
return 0
}
let x = f(n - 1, m)
return (m + x) % n
}
}