78[LeetCode] Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]


class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        int length=nums.size();
        sort(nums.begin(),nums.end());
        vector<vector<int> > result;
        for(int i=0;i<1<<length;i++)
        {
            vector<int> tmp;
        
            for(int j=0;j<length;j++)
            {
               
                if(i&1<<j)
                {
                    tmp.push_back(nums[j]);
                }
            }
            result.push_back(tmp);
        }
        return result;
    }


};

 

posted @ 2019-02-24 14:32  WhoDreamsSky  阅读(75)  评论(0编辑  收藏  举报