Skip to content

Commit f50b640

Browse files
committed
优化内存
1 parent a6a68ec commit f50b640

File tree

1 file changed

+11
-11
lines changed
  • Algorithms/17. Letter Combinations of a Phone Number

1 file changed

+11
-11
lines changed

Algorithms/17. Letter Combinations of a Phone Number/main.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,34 @@ class Solution {
2424
var results = [String]()
2525
for i in 0..<digits.characters.count {
2626
let cc = digits[digits.index(digits.startIndex, offsetBy: i)]
27-
results = combinations(results, Int(String(cc))!)
27+
combinations(&results, Int(String(cc))!)
2828
}
2929

3030
return results
3131
}
3232

33-
func combinations(_ results: [String], _ digit: Int) -> [String] {
33+
func combinations(_ results: inout [String], _ digit: Int) {
3434
let candidates = Solution.mapping[digit]
3535

3636
if results.isEmpty {
37-
return candidates
37+
results.append(contentsOf: candidates)
38+
return
3839
}
3940

40-
var newResults = [String]()
41-
for r in results {
42-
41+
let count = results.count
42+
for i in 0..<count {
4343
for cc in candidates {
44-
var rr = r
44+
var rr = results[i]
4545
rr.append(cc)
46-
newResults.append(rr)
46+
results.append(rr)
4747
}
4848
}
49-
50-
return newResults
49+
50+
results.removeFirst(count)
5151
}
5252

5353
}
5454

5555
let solution = Solution()
56-
print(solution.letterCombinations("2"))
56+
print(solution.letterCombinations("22344"))
5757

0 commit comments

Comments
 (0)