78.Subsets

78. Subsets

题目地址

https://leetcode.com/problems/subsets/

http://www.lintcode.com/en/problem/subsets/

题目描述

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

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

代码

Approach #1 DFS

数组排序的算法复杂度 O(nlogn)O(nlogn)

结果数为 2n2^n 个,所以时间复杂度为: O(2n)O(2^n)

使用临时数组list保存中间结果,所以空间复杂度为 O(n)O(n)

Approach 2: Recursion

Last updated

Was this helpful?