Clean C++ solution


  • 0
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> r;
            sort(nums.begin(), nums.end());
            auto n = int(pow(2, nums.size()));
    
            for (auto i = 0; i < n; ++i) {
                vector<int> subset;
                auto x = i, j = 0;
    
                while (x) {
                    if (x & 1) subset.push_back(nums[j]);
                    ++j;
                    x >>= 1;
                }
                r.push_back(subset);
            }
            return r;
        }
    };

Log in to reply
 

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