-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path90.subsets-ii.py
75 lines (70 loc) · 1.68 KB
/
90.subsets-ii.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# [90] Subsets II
#
# https://leetcode.com/problems/subsets-ii/description/
#
# algorithms
# Medium (39.90%)
# Total Accepted: 168.4K
# Total Submissions: 421.6K
# Testcase Example: '[1,2,2]'
#
# Given a collection of integers that might contain duplicates, nums, return
# all possible subsets (the power set).
#
# Note: The solution set must not contain duplicate subsets.
#
# Example:
#
#
# Input: [1,2,2]
# Output:
# [
# [2],
# [1],
# [1,2,2],
# [2,2],
# [1,2],
# []
# ]
#
#
#
# refer to 78.subset
# 24 ms, faster than 72.15%
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
results = []
tmp = []
self.helper(sorted(nums), tmp, 0, results)
return results
def helper(self, nums, tmp, start, results):
results.append(tmp[:])
for i in range(start, len(nums)):
if i > start and nums[i] == nums[i - 1]:
continue
else:
tmp.append(nums[i])
self.helper(nums, tmp, i + 1, results)
tmp.pop()
# refer to 78.subset
# 28 ms, faster than 42.58%
class Solution3(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
results = [[]]
size = len(nums)
sort_nums = sorted(nums)
for i in range(size):
if i == 0 or sort_nums[i] != sort_nums[i - 1]:
l = len(results)
for j in range(len(results) - l, len(results)):
tmp = results[j] + [sort_nums[i]]
results.append(tmp)