Leetcode 78 Solution

This article provides solution to leetcode question 78 (subsets)

https://leetcode.com/problems/subsets

Solution

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        int n = nums.size();
        int limit = pow(2, n);
        vector<vector<int>> res;

        for (int i = 0; i < limit; i++)
        {
            vector<int> v;
            for (int j = 0; j < n; j++)
            {
                if (i & (1 << j))
                    v.push_back(nums[j]);
            }

            res.push_back(v);
        }

        return res;
    }
};