Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0bf3f7

Browse files
D. J.:
- Added the leetcode problem and solution for 90
1 parent 77a9626 commit a0bf3f7

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
- [86 Partition List](https://leetcode.com/problems/partition-list/description/)
135135
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
136136
- [89 Gray Code](https://leetcode.com/problems/gray-code/description/)
137+
- [90 Subsets II](https://leetcode.com/problems/subsets-ii/description/)
137138
- [91 Decode Ways](https://leetcode.com/problems/decode-ways/description/)
138139
- [92 Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/description/)
139140
- [95 Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/description/)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
8+
"""
9+
Given an integer array nums that may contain duplicates, return all possible
10+
subsets (the power set).
11+
12+
A subset of an array is a selection of elements (possibly none) of the array.
13+
14+
The solution set must not contain duplicate subsets. Return the solution in any
15+
order.
16+
"""
17+
nums = sorted(nums)
18+
results = [[]]
19+
visited = set()
20+
for value in nums:
21+
for i in range(0, len(results)):
22+
new_subset = results[i] + [value]
23+
if tuple(new_subset) not in visited:
24+
visited.add(tuple(new_subset))
25+
results.append(new_subset)
26+
return results

‎tests/test_90_subsets_II.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._90_subsets_II import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 2, 2], [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]),
12+
([0], [[], [0]]),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: List[int]):
16+
"""Tests the solution of a LeetCode problem."""
17+
subsets = Solution().subsetsWithDup(nums)
18+
assert subsets == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /