Skip to content

Commit f551175

Browse files
committed
leetcode.com 46. Permutations
문제 링크: https://leetcode.com/problems/permutations
1 parent 4b48634 commit f551175

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

46. Permutations v3/main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def permute(self, nums: List[int]) -> List[List[int]]:
6+
answer = []
7+
visited = [False for _ in range(len(nums))]
8+
self.permutation(0, nums, visited, [], 0, answer)
9+
return answer
10+
11+
def permutation(self, bgn, nums, visited, building: List, depth, answer):
12+
if depth == len(nums):
13+
answer.append(building)
14+
return
15+
16+
for idx in range(len(nums)):
17+
if visited[idx]:
18+
continue
19+
20+
visited[idx] = True
21+
self.permutation(0, nums, visited, building + [nums[idx]], depth + 1, answer)
22+
visited[idx] = False

46. Permutations v3/test_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
class TestSolution(TestCase):
5+
def test1_permute(self):
6+
sln = Solution()
7+
self.assertEqual(
8+
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]],
9+
sln.permute(nums = [1,2,3])
10+
)

0 commit comments

Comments
 (0)