Leetcode 254 Solution

This article provides solution to leetcode question 254 (factor-combinations)

https://leetcode.com/problems/factor-combinations

Solution

class Solution {
    vector<vector<int>> res;
public:
    void getFactors(int n, int from, vector<int>& curr)
    {
        if (n == 1)
        {
            if (curr.size() > 1)
                res.push_back(curr);
            return;
        }

        for (int i = from; i <= n; i++)
        {
            if (n % i)
                continue;

            curr.push_back(i);
            getFactors(n / i, i, curr);
            curr.pop_back();
        }
    }

    vector<vector<int>> getFactors(int n) {
        vector<int> curr;
        getFactors(n, 2, curr);
        return res;
    }
};