From 21fdcf2f33143c9de76cbbcf21b3e32d2c226d58 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 27 May 2024 09:13:07 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 44 --- .../README.md" | 31 +++++++++++++++++++ .../Solution.swift" | 26 ++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" index 64ddca1e68cd6..a710289018c6a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" @@ -166,6 +166,37 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func findNthDigit(_ n: Int) -> Int { + var n = n + var k = 1 + var count = 9 + + while k * count < n { + n -= k * count + k += 1 + count *= 10 + } + + let num = Int(Double(10).power(Double(k - 1))) + (n - 1) / k + let idx = (n - 1) % k + let numString = String(num) + let char = numString[numString.index(numString.startIndex, offsetBy: idx)] + + return char.wholeNumberValue! + } +} + +extension Double { + func power(_ exponent: Double) -> Double { + return pow(self, exponent) + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/Solution.swift" new file mode 100644 index 0000000000000..a5146b3621822 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/Solution.swift" @@ -0,0 +1,26 @@ +class Solution { + func findNthDigit(_ n: Int) -> Int { + var n = n + var k = 1 + var count = 9 + + while k * count < n { + n -= k * count + k += 1 + count *= 10 + } + + let num = Int(Double(10).power(Double(k - 1))) + (n - 1) / k + let idx = (n - 1) % k + let numString = String(num) + let char = numString[numString.index(numString.startIndex, offsetBy: idx)] + + return char.wholeNumberValue! + } +} + +extension Double { + func power(_ exponent: Double) -> Double { + return pow(self, exponent) + } +} \ No newline at end of file From d075d82560e70e64cf244f77297b9b595f257300 Mon Sep 17 00:00:00 2001 From: klever34 Date: Mon, 27 May 2024 08:17:03 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" index a710289018c6a..9305b97a146e7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23044. \346\225\260\345\255\227\345\272\217\345\210\227\344\270\255\346\237\220\344\270\200\344\275\215\347\232\204\346\225\260\345\255\227/README.md" @@ -174,18 +174,18 @@ class Solution { var n = n var k = 1 var count = 9 - + while k * count < n { n -= k * count k += 1 count *= 10 } - + let num = Int(Double(10).power(Double(k - 1))) + (n - 1) / k let idx = (n - 1) % k let numString = String(num) let char = numString[numString.index(numString.startIndex, offsetBy: idx)] - + return char.wholeNumberValue! } }