Common dfs c++ solution


  • 0

    Idea is simple.

    vector<vector<int>> getFactors(int n) {
        vector<int> result;
        vector<vector<int>> results;
        getFactors(n, 2, result, results);
        return results;
    }
    
    void getFactors(int n, int start, vector<int> &result, vector<vector<int>> &results) {
        int factor = sqrt(n);
        for (int i = start; i <= factor; i++) {
            if (n % i == 0) {
                result.push_back(i);
                getFactors(n / i, i, result, results);
                result.pop_back();
            }
        }
        if (result.size()) {
            result.push_back(n);
            results.push_back(result);
            result.pop_back();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.