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
// @Title: 子集 (Subsets)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 4 ms
// @Memory: 6.5 MB
/*
 * @lc app=leetcode.cn id=78 lang=golang
 *
 * [78] 子集
 *
 * https://leetcode-cn.com/problems/subsets/description/
 *
 * algorithms
 * Medium (74.76%)
 * Likes:    339
 * Dislikes: 0
 * Total Accepted:    36K
 * Total Submissions: 48.1K
 * Testcase Example:  '[1,2,3]'
 *
 * 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
 *
 * 说明:解集不能包含重复的子集。
 *
 * 示例:
 *
 * 输入: nums = [1,2,3]
 * 输出:
 * [
 * ⁠ [3],
 * [1],
 * [2],
 * [1,2,3],
 * [1,3],
 * [2,3],
 * [1,2],
 * []
 * ]
 *
 */

func subsets(nums []int) [][]int {
	res := make([][]int, 1, 1024)

	for _, n := range nums {
		for _, r := range res {
			res = append(res, append([]int{n}, r...))
		}
	}
	return res
}