My different backtracking solution based on bit thinking


  • 0
    L
    void buildSubset(vector<vector<int>>& results,vector<int> &sol,vector<int> nums, int step)
    {
        int len=nums.size();
        if(step==len)
        {
            results.push_back(sol);
            return;
        }
        buildSubset(results,sol,nums,step+1);
            
        sol.push_back(nums[step]);
        buildSubset(results,sol,nums,step+1);
        sol.pop_back();
    
        
    }
    
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> ret;
        if(nums.empty()) return ret;
        std::sort(nums.begin(),nums.end());
        vector<int> sol;
        buildSubset(ret,sol,nums,0);
        return ret;
        
    }

Log in to reply
 

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