C++ solution with backtracking


  • 0
    Z

    Here is my c++ solution with the philosophy of backtracking:

        void subSetsRecur(vector<int>& nums, int sz, vector<vector<int> >& retvv) {
        if (sz == 0) {
            retvv.push_back(vector<int>());
            return;
        }
        subSetsRecur(nums, sz - 1, retvv);
        int sz2 = retvv.size();
        for (int i = 0; i < sz2; ++i) { // backtracking
            vector<int> vec = retvv[i];
            vec.push_back(nums[sz - 1]);
            retvv.insert(retvv.end(), vec);
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int> > ret;
        std::sort(nums.begin(), nums.end());
        
        subSetsRecur(nums, nums.size(), ret);
        return ret;
    }

Log in to reply
 

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