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-5/python/090_Subsets_II.py at master · finesure2017/leetcode-5

Expand file tree

/

090_Subsets_II.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
44 lines (43 loc) · 1.34 KB
/

090_Subsets_II.py

Copy path

File metadata and controls

44 lines (43 loc) · 1.34 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
class Solution(object):
# def subsetsWithDup(self, nums):
# """
# :type nums: List[int]
# :rtype: List[List[int]]
# """
# nums.sort()
# res = []
# for i in range(1 << len(nums)):
# res.append(self.get_subsets(nums, i))
# # remove duplicate
# final_res = {}
# for subset in res:
# hash_key = ''.join([str(t) for t in subset])
# try:
# final_res[hash_key]
# except:
# final_res[hash_key] = subset
# return final_res.values()
#
# 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 subsetsWithDup(self, nums):
nums.sort()
res = [[]]
begin = 0
for index in range(len(nums)):
if index == 0 or nums[index] != nums[index - 1]:
# generate all
begin = 0
size = len(res)
# use existing subsets to generate new subsets
for j in range(begin, size):
curr = list(res[j])
curr.append(nums[index])
res.append(curr)
# avoid duplicate subsets
begin = size
return res
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.