mirror of https://github.com/doocs/leetcode.git
feat: add swift implementation to lcof problem: No.62 (#2954)
This commit is contained in:
parent
62923268ff
commit
5fe38660fb
|
|
@ -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 -->
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue