Skip to content

Commit

Permalink
优化内存
Browse files Browse the repository at this point in the history
  • Loading branch information
aelam committed May 10, 2017
1 parent a6a68ec commit f50b640
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Algorithms/17. Letter Combinations of a Phone Number/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ class Solution {
var results = [String]()
for i in 0..<digits.characters.count {
let cc = digits[digits.index(digits.startIndex, offsetBy: i)]
results = combinations(results, Int(String(cc))!)
combinations(&results, Int(String(cc))!)
}

return results
}

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

if results.isEmpty {
return candidates
results.append(contentsOf: candidates)
return
}

var newResults = [String]()
for r in results {

let count = results.count
for i in 0..<count {
for cc in candidates {
var rr = r
var rr = results[i]
rr.append(cc)
newResults.append(rr)
results.append(rr)
}
}

return newResults
results.removeFirst(count)
}

}

let solution = Solution()
print(solution.letterCombinations("2"))
print(solution.letterCombinations("22344"))

0 comments on commit f50b640

Please sign in to comment.