Skip to content

Commit ff2b230

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 47
1 parent 882a858 commit ff2b230

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
- [42 Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)
101101
- [45 Jump Game II](https://leetcode.com/problems/jump-game-ii/description/)
102102
- [46 Permutations](https://leetcode.com/problems/permutations/description/)
103+
- [47 Permutations II](https://leetcode.com/problems/permutations-ii/description/)
103104
- [48 Rotate Image](https://leetcode.com/problems/rotate-image/description/)
104105
- [49 Group Anagrams](https://leetcode.com/problems/group-anagrams/description/)
105106
- [50 Pow(x_n)](https://leetcode.com/problems/powx-n/description/)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
8+
"""
9+
Given a collection of numbers, nums, that might contain duplicates, return all
10+
possible unique permutations in any order.
11+
"""
12+
res = []
13+
cache = set()
14+
15+
def dfs(pair: List[int], rest: List[int]):
16+
if tuple(pair) in cache:
17+
return
18+
if not rest:
19+
res.append(pair)
20+
cache.add(tuple(pair))
21+
return
22+
for i, n in enumerate(rest):
23+
new_pair = pair + [n]
24+
new_rest = rest[:i] + rest[i + 1 :]
25+
dfs(new_pair, new_rest)
26+
27+
dfs([], nums)
28+
return res

tests/test_47_permutations_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._47_permutations_II import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 1, 2], [[1, 1, 2], [1, 2, 1], [2, 1, 1]]),
12+
([1, 2, 3], [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: List[List[int]]):
16+
"""Tests the solution of a LeetCode problem."""
17+
permutations = Solution().permuteUnique(nums)
18+
assert permutations == expected

0 commit comments

Comments
 (0)