Share my short c++ solution


  • 0
    O
    class Solution {
    public:
        vector<vector<int>> getFactors(int n) {
            vector<vector<int> > ans;
            for (int i = 2; i * i <= n; i++) {
                if (n % i != 0) {
                    continue;
                }
                ans.push_back(vector<int> {i, n / i});
                auto tmp = getFactors(n / i);
                for (auto j : tmp) {
                    j.push_back(i);
                    sort(j.begin(), j.end());
                    if (find(ans.begin(), ans.end(), j) == ans.end()) {
                        ans.push_back(j);
                    }
                }
            }
            return ans;
        }
    };

Log in to reply
 

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