BACKTRACKING C++ use the answer of combination


  • 0
    H
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> res;
            vector<int> pot;
            for(int i = 0 ; i <= nums.size();++i){
                subsets(nums,res, pot,0,i);
            }
            return res;
        }
         void subsets(vector<int>& nums, vector<vector<int>>& res,  vector<int>& pot,int start,int count) {
             if(!count)
             {
                 res.push_back(pot);
                 return;
             }
             
             for(int i = start ; i < nums.size();++i){
                 pot.push_back(nums[i]);
                 subsets(nums,res,pot,i+1,count-1);
                 pot.pop_back();
             }
         }
    };

Log in to reply
 

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