File tree Expand file tree Collapse file tree 1 file changed +11
-11
lines changed
Algorithms/17. Letter Combinations of a Phone Number Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -24,34 +24,34 @@ class Solution {
24
24
var results = [ String] ( )
25
25
for i in 0 ..< digits. characters. count {
26
26
let cc = digits [ digits. index ( digits. startIndex, offsetBy: i) ]
27
- results = combinations ( results, Int ( String ( cc) ) !)
27
+ combinations ( & results, Int ( String ( cc) ) !)
28
28
}
29
29
30
30
return results
31
31
}
32
32
33
- func combinations( _ results: [ String ] , _ digit: Int ) -> [ String ] {
33
+ func combinations( _ results: inout [ String ] , _ digit: Int ) {
34
34
let candidates = Solution . mapping [ digit]
35
35
36
36
if results. isEmpty {
37
- return candidates
37
+ results. append ( contentsOf: candidates)
38
+ return
38
39
}
39
40
40
- var newResults = [ String] ( )
41
- for r in results {
42
-
41
+ let count = results. count
42
+ for i in 0 ..< count {
43
43
for cc in candidates {
44
- var rr = r
44
+ var rr = results [ i ]
45
45
rr. append ( cc)
46
- newResults . append ( rr)
46
+ results . append ( rr)
47
47
}
48
48
}
49
-
50
- return newResults
49
+
50
+ results . removeFirst ( count )
51
51
}
52
52
53
53
}
54
54
55
55
let solution = Solution ( )
56
- print ( solution. letterCombinations ( " 2 " ) )
56
+ print ( solution. letterCombinations ( " 22344 " ) )
57
57
You can’t perform that action at this time.
0 commit comments