forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactor_Combinations.cpp
More file actions
26 lines (25 loc) · 834 Bytes
/
Factor_Combinations.cpp
File metadata and controls
26 lines (25 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
void getFactorsImpl(int n, vector<int>& solution, vector<vector<int>>& result) {
if(!solution.empty()) {
solution.push_back(n);
result.push_back(solution);
solution.pop_back();
}
for(int i = solution.empty() ? 2 : max(2, solution[solution.size() - 1]); i <= (int)floor(sqrt(n)); ++i) {
if(n % i == 0) {
int factor = n / i;
if(factor < i) break;
solution.push_back(i);
getFactorsImpl(factor, solution, result);
solution.pop_back();
}
}
}
public:
vector<vector<int>> getFactors(int n) {
vector<int> solution;
vector<vector<int>> result;
getFactorsImpl(n, solution, result);
return result;
}
};