17 lines concise and easy understand c++ solution backtracking


  • 0
    A
    class Solution {
    public:
        vector<vector<int>> getFactors(int n) {
            vector<vector<int>> res;
            vector<int> tmp;
            backtracking(res, tmp, n, 2);
            return res;
        }
        void backtracking(vector<vector<int>>& res, vector<int> tmp, int n, int index){
            if(n == 1){
                if(tmp.size() > 1) res.push_back(tmp);
                return;
            }
            for(int i = index; i <= n; i++){
                if(n % i == 0){
                    tmp.push_back(i);
                    backtracking(res, tmp, n/i, i);
                    tmp.pop_back();
                }
            }
        }
    };

Log in to reply
 

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