GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/078_Subsets.py at master · Alex220284/leetcode

Expand file tree

/

078_Subsets.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
58 lines (52 loc) · 1.72 KB
/

078_Subsets.py

Copy path

File metadata and controls

58 lines (52 loc) · 1.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution(object):
# def subsets(self, nums):
# """
# :type nums: List[int]
# :rtype: List[List[int]]
# """
# nums.sort()
# res = [[]]
# res.append(list(nums))
# for ls in range(1, len(nums)):
# self.get_subsets(res, nums, [], ls, 0)
# return res
#
# def get_subsets(self, res, nums, curr_set, ls, index):
# # recursive
# if ls == 0:
# res.append(list(curr_set))
# elif index < len(nums):
# curr_set.append(nums[index])
# self.get_subsets(res, nums, curr_set, ls - 1, index + 1)
# curr_set.pop()
# self.get_subsets(res, nums, curr_set, ls, index + 1)
# def subsets(self, nums):
# # https://leetcode.com/discuss/89343/c-8-lines-bit-operation
# # doesn't work when len(nums) > 32
# nums.sort()
# res = []
# for i in range(1 << len(nums)):
# res.append(self.get_subsets(nums, i))
# return res
#
# def get_subsets(self, nums, magic):
# res = []
# for i in range(len(nums)):
# if (1 << i) & magic != 0:
# res.append(nums[i])
# return res
def subsets(self, nums):
# Sort and iteratively generate n subset with n-1 subset, O(n^2) and O(2^n)
nums.sort()
res = [[]]
for index in range(len(nums)):
size = len(res)
# use existing subsets to generate new subsets
for j in range(size):
curr = list(res[j])
curr.append(nums[index])
res.append(curr)
return res
if __name__ == "__main__":
s = Solution()
print s.subsets([1,2,3])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.