Skip to content

Commit e10a4b6

Browse files
committed
update readme
1 parent a4794e6 commit e10a4b6

File tree

5 files changed

+59
-128
lines changed

5 files changed

+59
-128
lines changed

0078-subsets/0078-subsets.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

0078-subsets/README.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

Python/0078-subsets.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
11
# time complexity: O(n*2^n)
2-
# space complexity: O(n*2^n)
2+
# space complexity: O(n)
33
from itertools import chain, combinations
44
from typing import List
55

66

7+
class Solution:
8+
def subsets(self, nums: List[int]) -> List[List[int]]:
9+
result = []
10+
11+
def backtrack(start: int, comb: List[int]):
12+
result.append(list(comb))
13+
for i in range(start, len(nums)):
14+
comb.append(nums[i])
15+
backtrack(i + 1, comb)
16+
comb.pop()
17+
18+
backtrack(0, [])
19+
return result
20+
21+
# time complexity: O(n*2^n)
22+
# space complexity: O(n*2^n)
23+
class Solution:
24+
def subsets(self, nums):
25+
result = [[]]
26+
for num in nums:
27+
newSubsets = []
28+
for curr in result:
29+
temp = curr.copy()
30+
temp.append(num)
31+
newSubsets.append(temp)
32+
for curr in newSubsets:
33+
result.append(curr)
34+
return result
35+
36+
# time complexity: O(n*2^n)
37+
# space complexity: O(n)
38+
class Solution:
39+
def subsets(self, nums: List[int]) -> List[List[int]]:
40+
n = len(nums)
41+
result = []
42+
43+
for i in range(2**n, 2 ** (n + 1)):
44+
# generate bitmask, from 0..00 to 1..11
45+
bitmask = bin(i)[3:]
46+
# append subset corresponding to that bitmask
47+
result.append([nums[j] for j in range(n) if bitmask[j] == "1"])
48+
49+
return result
50+
51+
752
class Solution:
853
def subsets(self, nums: List[int]) -> List[List[int]]:
954
result = list(chain.from_iterable(combinations(nums, r)
@@ -12,16 +57,16 @@ def subsets(self, nums: List[int]) -> List[List[int]]:
1257
result[i] = list(item)
1358
return result
1459

15-
# class Solution:
16-
# def subsets(self, nums: List[int]) -> List[List[int]]:
17-
# output = [[]]
18-
19-
# for num in nums:
20-
# output += [curr + [num] for curr in output]
2160

22-
# return output
61+
class Solution:
62+
def subsets(self, nums: List[int]) -> List[List[int]]:
63+
output = [[]]
64+
for num in nums:
65+
output += [curr + [num] for curr in output]
66+
return output
2367

2468

2569
nums = [1, 2, 3]
26-
70+
print(Solution().subsets(nums))
71+
nums = [0]
2772
print(Solution().subsets(nums))

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,3 @@ It helps others discover the repo and keeps the project growing.
115115
---
116116

117117
Feedback / Questions → open an Issue or reach out on [LinkedIn](https://www.linkedin.com/in/hogan-l/)
118-
119-
<!---LeetCode Topics Start-->
120-
# LeetCode Topics
121-
## Array
122-
| |
123-
| ------- |
124-
| [0078-subsets](https://github.com/hogan-tech/leetcode-solution/tree/master/0078-subsets) |
125-
## Backtracking
126-
| |
127-
| ------- |
128-
| [0078-subsets](https://github.com/hogan-tech/leetcode-solution/tree/master/0078-subsets) |
129-
## Bit Manipulation
130-
| |
131-
| ------- |
132-
| [0078-subsets](https://github.com/hogan-tech/leetcode-solution/tree/master/0078-subsets) |
133-
<!---LeetCode Topics End-->

Readme/0078-subsets.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
<h2><a href="https://leetcode.com/problems/subsets/">78. Subsets</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code> of <strong>unique</strong> elements, return <em>all possible</em> <span data-keyword="subset"><em>subsets</em></span> <em>(the power set)</em>.</p>
1+
<h2><a href="https://leetcode.com/problems/subsets">78. Subsets</a></h2><h3>Medium</h3><hr><p>Given an integer array <code>nums</code> of <strong>unique</strong> elements, return <em>all possible</em> <span data-keyword="subset"><em>subsets</em></span> <em>(the power set)</em>.</p>
22

33
<p>The solution set <strong>must not</strong> contain duplicate subsets. Return the solution in <strong>any order</strong>.</p>
44

55
<p>&nbsp;</p>
66
<p><strong class="example">Example 1:</strong></p>
77

8-
<pre><strong>Input:</strong> nums = [1,2,3]
8+
<pre>
9+
<strong>Input:</strong> nums = [1,2,3]
910
<strong>Output:</strong> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
1011
</pre>
1112

1213
<p><strong class="example">Example 2:</strong></p>
1314

14-
<pre><strong>Input:</strong> nums = [0]
15+
<pre>
16+
<strong>Input:</strong> nums = [0]
1517
<strong>Output:</strong> [[],[0]]
1618
</pre>
1719

@@ -23,4 +25,3 @@
2325
<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
2426
<li>All the numbers of&nbsp;<code>nums</code> are <strong>unique</strong>.</li>
2527
</ul>
26-
</div>

0 commit comments

Comments
 (0)