# 78     Subsets

## 78. [Subsets](https://leetcode.com/problems/subsets/description/)

## 1. Question

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

**Note:**&#x54;he solution set must not contain duplicate subsets.

For example,\
If**nums**=`[1,2,3]`, a solution is:

```
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
```

## 2. Implementation

**(1) Backtracking**

```java
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> subset = new ArrayList<>();
        getSubsets(nums, 0, subset, res);
        return res;
    }

    public void getSubsets(int[] nums, int start, List<Integer> subset, List<List<Integer>> res) {
        res.add(new ArrayList<>(subset));

        for (int i = start; i < nums.length; i++) {
            subset.add(nums[i]);
            getSubsets(nums, i + 1, subset, res);
            subset.remove(subset.size() - 1);
        }
    }
}
```

## 3. Time & Space Complexity

**Backtracking:** 时间复杂度O(2^n), 空间复杂度O(2^n)
