[LeetCode] Subsets

78. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
res.push_back({});
for(auto& n: nums){
int sz = res.size();
for(int i = 0; i < sz; i++){
vector<int> tmp = res[i];
tmp.push_back(n);
res.push_back(tmp);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/06/PS/LeetCode/subsets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.