Just 2 operation, select or not select


  • 0
    E

    class Solution {
    public:
    vector<vector<int>> subsets(vector<int>& nums) {
    vector<vector<int> > ans;
    for( int i = 0; i < nums.size(); i++ ){
    if( i == 0 ){ // initialization
    vector<int> v;
    ans.push_back(v);
    v.push_back(nums[i]);
    ans.push_back(v);
    }
    else{
    // note that you do not have to perform the not select operation, since previous results already do not select current number
    int prevSize = ans.size();
    vector<int> v;
    for( int k = 0; k < prevSize; k++ ){
    v = ans[k];
    v.push_back(nums[i]);
    ans.push_back(v); // select current number
    }
    }
    }
    return ans;
    }
    };


Log in to reply
 

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