Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bec93e5

Browse files
#78. Subsets
1 parent ac88c8d commit bec93e5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎Backtracking/subsets.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Given an integer array nums of unique elements, return all possible
2+
# subsets
3+
# (the power set).
4+
5+
# The solution set must not contain duplicate subsets. Return the solution
6+
# in any order.
7+
8+
9+
# Example 1:
10+
# Input: nums = [1,2,3]
11+
# Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
12+
13+
# Example 2:
14+
# Input: nums = [0]
15+
# Output: [[],[0]]
16+
17+
# Constraints:
18+
19+
# 1 <= nums.length <= 10
20+
# -10 <= nums[i] <= 10
21+
# All the numbers of nums are unique.
22+
23+
24+
from typing import List
25+
class Solution:
26+
def subsets(self, nums: List[int]) -> List[List[int]]:
27+
ans = []
28+
def dfs(s, path):
29+
ans.append(path)
30+
for i in range(s, len(nums)):
31+
dfs(i+1, path+[nums[i]])
32+
dfs(0, [])
33+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /