Short c++ iterative solution


  • 0
    R
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> ret;
        ret.push_back(vector<int>());
        sort(nums.begin(), nums.end());
        for (const auto& i:nums) {
            vector<vector<int>> sub(ret);
            for (auto& j:sub) j.push_back(i);
            ret.insert(ret.end(), sub.begin(), sub.end());
        }
        return ret;
    }

Log in to reply
 

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