-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubSets.py
More file actions
34 lines (32 loc) · 794 Bytes
/
subSets.py
File metadata and controls
34 lines (32 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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],
# []
# ]
class Solution:
"""
@param nums: A set of numbers
@return: A list of lists
"""
def subsets(self, nums):
if nums is None:
return
self.results = []
self.search(sorted(nums), [], 0)
return self.results
def search(self, nums, subset, startIndex):
self.results.append(list(subset))
for i in xrange(startIndex, len(nums)):
subset.append(nums[i])
self.search(nums, subset, i+1)
subset.pop()