File tree 2 files changed +57
-0
lines changed
2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,37 @@ public class Solution {
166
166
}
167
167
```
168
168
169
+ #### Swift
170
+
171
+ ``` swift
172
+ class Solution {
173
+ func findNthDigit (_ n : Int ) -> Int {
174
+ var n = n
175
+ var k = 1
176
+ var count = 9
177
+
178
+ while k * count < n {
179
+ n -= k * count
180
+ k += 1
181
+ count *= 10
182
+ }
183
+
184
+ let num = Int (Double (10 ).power (Double (k - 1 ))) + (n - 1 ) / k
185
+ let idx = (n - 1 ) % k
186
+ let numString = String (num)
187
+ let char = numString[numString.index (numString.startIndex , offsetBy : idx)]
188
+
189
+ return char.wholeNumberValue !
190
+ }
191
+ }
192
+
193
+ extension Double {
194
+ func power (_ exponent : Double ) -> Double {
195
+ return pow (self , exponent)
196
+ }
197
+ }
198
+ ```
199
+
169
200
<!-- tabs: end -->
170
201
171
202
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func findNthDigit( _ n: Int ) -> Int {
3
+ var n = n
4
+ var k = 1
5
+ var count = 9
6
+
7
+ while k * count < n {
8
+ n -= k * count
9
+ k += 1
10
+ count *= 10
11
+ }
12
+
13
+ let num = Int ( Double ( 10 ) . power ( Double ( k - 1 ) ) ) + ( n - 1 ) / k
14
+ let idx = ( n - 1 ) % k
15
+ let numString = String ( num)
16
+ let char = numString [ numString. index ( numString. startIndex, offsetBy: idx) ]
17
+
18
+ return char. wholeNumberValue!
19
+ }
20
+ }
21
+
22
+ extension Double {
23
+ func power( _ exponent: Double ) -> Double {
24
+ return pow ( self , exponent)
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments