Skip to content

Commit 0d7b4c4

Browse files
committed
[DFS] Refactor code style of Factor Combinations
1 parent dc2cae9 commit 0d7b4c4

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

DFS/FactorCombinations.swift

+9-12
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,27 @@
88

99
class FactorCombinations {
1010
func getFactors(_ n: Int) -> [[Int]] {
11-
var res = [[Int]]()
12-
var path = [Int]()
11+
var paths = [[Int]](), path = [Int]()
1312

14-
dfs(&res, &path, n, 2)
13+
dfs(&paths, path, 2, n)
1514

16-
return res
15+
return paths
1716
}
1817

19-
private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ n: Int, _ start: Int) {
20-
if n == 1 {
18+
private func dfs(_ paths: inout [[Int]], _ path: [Int], _ start: Int, _ target: Int) {
19+
if target == 1 {
2120
if path.count > 1 {
22-
res.append(Array(path))
21+
paths.append(path)
2322
}
2423
return
2524
}
2625

27-
if start > n {
26+
guard start <= target else {
2827
return
2928
}
3029

31-
for i in start...n where n % i == 0 {
32-
path.append(i)
33-
dfs(&res, &path, n / i, i)
34-
path.removeLast()
30+
for factor in start...target where target % factor == 0 {
31+
dfs(&paths, path + [factor], factor, target / factor)
3532
}
3633
}
3734
}

0 commit comments

Comments
 (0)