LeetCode 0078 - Subsets

2021-08-11 10:51:11 浏览数 (1)

Subsets

Desicription

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

Note: The solution set must not contain duplicate subsets.

For example,

If nums = [1,2,3], a solution is:

代码语言:javascript复制
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solution

代码语言:javascript复制
class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        int nums_size = nums.size();
        int res_size = pow(2, nums_size);
        vector<vector<int>> res(res_size);
        for(int i = 0; i < nums_size; i  )
            for(int j = 0; j < res_size; j  )
                if((j >> i) & 1)
                    res[j].push_back(nums[i]);
        return res;
    }
};

0 人点赞