All problems

Subsets

Medium · backtracking

Given a list nums of distinct integers, return every possible subset, including the empty subset and the full list itself.

Inside each subset, keep the numbers in the same order they have in nums. The subsets themselves may be returned in any order. The result must contain no duplicate subsets.

Examples

Example 1

Input: nums = [1, 2, 3]

Output: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

Eight subsets in total, 2^3. Their order in the answer does not matter.

Example 2

Input: nums = [5, -1]

Output: [[], [5], [-1], [5, -1]]

Inside [5, -1] the numbers keep their order from nums, so it is never written [-1, 5].

Example 3

Input: nums = [0]

Output: [[], [0]]

Constraints