c++ easy to understand backtracking solution beats 79%


  • 2
    S
    void sub(vector<vector<int>> &ans, vector<int> &sets, vector<int> &A, int j){
        if(j>=A.size()){
            return;
        }
        for(int i=j;i<A.size();i++){
            if(i>j && A[i]==A[i-1])
            continue;
            sets.push_back(A[i]);
            ans.push_back(sets);
            sub(ans,sets,A,i+1);
            sets.pop_back();
        }
        
    }
    vector<vector<int>> subsetsWithDup(vector<int>& A) {
        vector<vector<int>> ans;
        vector<int> sets;
        sort(A.begin(),A.end());
        ans.push_back(sets);
        sub(ans,sets,A,0);
        return ans;
    }
    

    };
    '''


Log in to reply
 

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