diff --git "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" index 0f0b6c9a4656e..66fe688f2f6b8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" @@ -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 + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.swift" new file mode 100644 index 0000000000000..4ba243f5dbab2 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.swift" @@ -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 + } +} \ No newline at end of file