Generate every possible subset of a given list of distinct integers.
Problem
Given a list of distinct integers, return the power set: every subset that can be formed from the list, including the empty subset and the full set.
A subset may be returned in any order, but each subset should contain elements in the same relative order as they appear in the input.
Goal
Produce all subsets without duplicates.
Input Format
- A list
numsof distinct integers. nums.length = n.- Each element may be positive, negative, or zero.
Output Format
- Return a list of all subsets of
nums. - Each subset is represented as a list of integers.
- The collection of subsets may be in any order.
Constraints
- is a reasonable interview-scale assumption.
- Elements are distinct.
- The total number of subsets is .
Example 1
Input
nums = [1,2,3]
Output
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Explanation
Each element can either be present or absent, producing subsets.
Example 2
Input
nums = [0]
Output
[[],[0]]
Explanation
There are two subsets: the empty subset and the subset containing the single element.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.