C++ solution with Bit Manipulation, easy to understand, you don't need to waste time to find the regular.


  • 0
    C
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            int max=1<<nums.size(),n=nums.size()-1;
            vector<vector<int>> result;
            for(int i=0;i<max;i++)
            {
                int mask=1;
                vector<int> vec;
                for(int j=nums.size()-1;j>=0;j--)
                {
                    if(i&(mask<<j)) vec.push_back(nums[n-j]);
                }
                result.push_back(vec);
            }
            return result;
        }
    };

Log in to reply
 

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