File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,48 @@ public class Solution {
212212}
213213```
214214
215+ #### Swift
216+
217+ ``` swift
218+ class Solution {
219+ func printNumbers (_ n : Int ) -> [Int ] {
220+ let maxNumber = maxNumberForDigits (n)
221+ return Array (1 ... maxNumber )
222+ }
223+
224+ private func maxNumberForDigits (_ n : Int ) -> Int {
225+ var maxNumber = 1
226+ for _ in 0 ..< n {
227+ maxNumber *= 10
228+ }
229+ return maxNumber - 1
230+ }
231+
232+ private var s = String ()
233+ private var ans = [String ]()
234+
235+ func print (_ n : Int ) -> [String ] {
236+ for i in 1 ... n {
237+ dfs (0 , i)
238+ }
239+ return ans
240+ }
241+
242+ private func dfs (_ i : Int , _ j : Int ) {
243+ if i == j {
244+ ans.append (s)
245+ return
246+ }
247+ let start = i > 0 ? 0 : 1
248+ for k in start..< 10 {
249+ s.append (" \( k ) " )
250+ dfs (i + 1 , j)
251+ s.removeLast ()
252+ }
253+ }
254+ }
255+ ```
256+
215257<!-- tabs:end -->
216258
217259<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func printNumbers( _ n: Int ) -> [ Int ] {
3+ let maxNumber = maxNumberForDigits ( n)
4+ return Array ( 1 ... maxNumber)
5+ }
6+
7+ private func maxNumberForDigits( _ n: Int ) -> Int {
8+ var maxNumber = 1
9+ for _ in 0 ..< n {
10+ maxNumber *= 10
11+ }
12+ return maxNumber - 1
13+ }
14+
15+ private var s = String ( )
16+ private var ans = [ String] ( )
17+
18+ func print( _ n: Int ) -> [ String ] {
19+ for i in 1 ... n {
20+ dfs ( 0 , i)
21+ }
22+ return ans
23+ }
24+
25+ private func dfs( _ i: Int , _ j: Int ) {
26+ if i == j {
27+ ans. append ( s)
28+ return
29+ }
30+ let start = i > 0 ? 0 : 1
31+ for k in start..< 10 {
32+ s. append ( " \( k) " )
33+ dfs ( i + 1 , j)
34+ s. removeLast ( )
35+ }
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments