Super Intuitive Subsets I&II


  • 0
    K
    vector<vector<int>> subsets(vector<int>& nums) 
        {
            vector<vector<int>> res;
            res.push_back(vector<int>());
            for (auto num : nums) {
                for (int i = 0, end = res.size(); i < end; ++i) {
                    res.push_back(res[i]);
                    res.back().push_back(num);
                }
            }
            return res;
        }
    
    vector<vector<int> > subsetsWithDup(vector<int> &nums) {
            vector<vector<int>> res;
            sort(nums.begin(), nums.end());
            res.push_back(vector<int>());
            for (int k = 0, preIdx = 0; k < nums.size(); ++k) {
                int i = (k && nums[k] == nums[k-1]) ? preIdx : 0;
                preIdx = res.size();
                for (int end = res.size(); i < end; ++i) {
                    res.push_back(res[i]);
                    res.back().push_back(nums[k]);
                }
            }
            return res;
        }
    

Log in to reply
 

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