2024-05-21 Daily Challenge

Today I have done leetcode's May LeetCoding Challenge with cpp.

May LeetCoding Challenge 21

Description

Subsets

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

 

Example 1:

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

Example 2:

Input: nums = [0]
Output: [[],[0]]

 

Constraints:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All the numbers of nums are unique.

Solution

auto speedup = [](){
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  return 0;
}();
class Solution {
public:
  vector<vector<int>> subsets(vector<int>& nums) {
    int len = nums.size();
    int sz = (1 << len);
    vector<vector<int>> answer;
    for(int i = 0; i < sz; ++i) {
      vector<int> st;
      for(int b = 0; b < len; ++b) {
        if(i & (1 << b)) st.push_back(nums[b]);
      }
      answer.emplace_back(move(st));
    }
    return answer;
  }
};

// Accepted
// 10/10 cases passed (7 ms)
// Your runtime beats 26.93 % of cpp submissions
// Your memory usage beats 85.44 % of cpp submissions (7.1 MB)