Generate all possible subsets from a given set of distinct integers.

Given a set of distinct integers, this algorithm will return all possible subsets. For example, if the input is [1,2,3], the output will be [[],[1],[ 2],[3],[1,2],[1,3],[2,3],[1,2,3]].

Problem

Given a set of distinct integers, return all possible subsets.
Example input:
[1,2,3]
Example output:
[[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def subsets(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    res = [[]]
    for num in nums:
        res += [item + [num] for item in res]
    return res

print(subsets([1,2,3]))

A.I. Evaluation of the Solution

This is a great solution! It is complete, and correctly solves the problem. The approach is also very elegant.

Evaluated at: 2022-12-07 10:15:51