From 0d7b4c42a5a0161ef5c7f90005e96b271acf58bb Mon Sep 17 00:00:00 2001 From: Yi Gu Date: Fri, 28 Feb 2020 11:55:34 -0800 Subject: [PATCH] [DFS] Refactor code style of Factor Combinations --- DFS/FactorCombinations.swift | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/DFS/FactorCombinations.swift b/DFS/FactorCombinations.swift index 19f7d05c..e4941901 100644 --- a/DFS/FactorCombinations.swift +++ b/DFS/FactorCombinations.swift @@ -8,30 +8,27 @@ class FactorCombinations { func getFactors(_ n: Int) -> [[Int]] { - var res = [[Int]]() - var path = [Int]() + var paths = [[Int]](), path = [Int]() - dfs(&res, &path, n, 2) + dfs(&paths, path, 2, n) - return res + return paths } - private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ n: Int, _ start: Int) { - if n == 1 { + private func dfs(_ paths: inout [[Int]], _ path: [Int], _ start: Int, _ target: Int) { + if target == 1 { if path.count > 1 { - res.append(Array(path)) + paths.append(path) } return } - if start > n { + guard start <= target else { return } - for i in start...n where n % i == 0 { - path.append(i) - dfs(&res, &path, n / i, i) - path.removeLast() + for factor in start...target where target % factor == 0 { + dfs(&paths, path + [factor], factor, target / factor) } } } \ No newline at end of file