From def21878010c993cbce059e91afcede290da95ff Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 29 May 2024 08:34:21 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 62 --- .../README.md" | 18 ++++++++++++++++++ .../Solution.swift" | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 "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" 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..441dfebe3255e 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 From 0884b46da3bb05ebab16c3597e1e4ec817af59a0 Mon Sep 17 00:00:00 2001 From: klever34 Date: Wed, 29 May 2024 07:36:19 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 441dfebe3255e..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" @@ -168,7 +168,7 @@ 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