일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Protocol
- data science
- 시바견
- Python
- 운영체제
- Class
- 43. Multiply Strings
- Regular Expression
- LeetCode
- 프로그래머스
- Substring with Concatenation of All Words
- 715. Range Module
- Generator
- DWG
- Convert Sorted List to Binary Search Tree
- 밴픽
- Python Implementation
- kaggle
- Python Code
- 파이썬
- 컴퓨터의 구조
- concurrency
- shiba
- 315. Count of Smaller Numbers After Self
- attribute
- 109. Convert Sorted List to Binary Search Tree
- Decorator
- 30. Substring with Concatenation of All Words
- iterator
- t1
Archives
- Today
- Total
Scribbling
LeetCode: 46. Permutations 본문
DFS Solution.
from itertools import permutations
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
# Using Library
# return list(permutations(nums, len(nums)))
ret = []
self.helper(nums, [], ret)
return ret
def helper(self, nums, element, ret):
if not nums:
ret.append(element)
for i, num in enumerate(nums):
self.helper(nums[:i]+nums[i+1:], element+[num], ret)
A lazy solution using generator.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
self.ret = []
for out in self.gen(nums):
self.ret.append(out)
return self.ret
def gen(self, nums):
if not nums:
yield []
for i, num in enumerate(nums):
for out in self.gen(nums[:i]+nums[i+1:]):
yield [num] + out
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 41. First Missing Positive (0) | 2021.09.08 |
---|---|
LeetCode: 47. Permutations II (0) | 2021.09.07 |
LeetCode: 40. Combination Sum II (0) | 2021.09.07 |
37. Sudoku Solver (0) | 2021.09.06 |
36. Valid Sudoku (0) | 2021.09.06 |