일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 밴픽
- Substring with Concatenation of All Words
- Python Implementation
- 컴퓨터의 구조
- Decorator
- t1
- Protocol
- Generator
- Class
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- 43. Multiply Strings
- DWG
- attribute
- 프로그래머스
- data science
- 시바견
- 315. Count of Smaller Numbers After Self
- LeetCode
- Regular Expression
- 파이썬
- Python
- kaggle
- Python Code
- concurrency
- 109. Convert Sorted List to Binary Search Tree
- iterator
- 운영체제
- shiba
- 715. Range Module
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 |