-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path78.子集.cpp
54 lines (52 loc) · 1.05 KB
/
78.子集.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode.cn id=78 lang=cpp
*
* [78] 子集
*
* https://leetcode-cn.com/problems/subsets/description/
*
* algorithms
* Medium (74.97%)
* Likes: 367
* Dislikes: 0
* Total Accepted: 41.7K
* Total Submissions: 55.6K
* Testcase Example: '[1,2,3]'
*
* 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
*
* 说明:解集不能包含重复的子集。
*
* 示例:
*
* 输入: nums = [1,2,3]
* 输出:
* [
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
*
*/
// @lc code=start
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> ans(1);
for(int i=0; i<nums.size(); i++){
int pre = ans.size();
for(int j=0; j<pre; j++){
vector<int>tmp = ans[j];
tmp.push_back(nums[i]);
ans.push_back(tmp);
}
}
return ans;
}
};
// @lc code=end