Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 디자인패턴
- jwt
- 프로그래머스
- spring batch 5.0
- 파이썬
- JavaScript
- KPT회고
- 프로그래머스 이중우선순위큐
- 커스텀 헤더
- @FeignClient
- 1주일회고
- 코딩테스트 준비
- Spring multimodule
- 전략패턴 #StrategyPattern #디자인패턴
- 인프콘 2024
- 개발자 취업
- 단기개발자코스
- 항해99
- infcon 2024
- 99클럽
- 디자인 패턴
- 취업리부트코스
- 구글 OAuth login
- 개발자부트캠프추천
- 빈 조회 2개 이상
- DesignPattern
- 빈 충돌
- TiL
- Python
- jwttoken
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 46.Permutations(순열) with Python 본문
https://leetcode.com/problems/permutations/
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
answer = []
def dfs(pair, n):
if len(pair) == n:
answer.append(pair[:])
return
for i in nums:
if i not in pair:
pair.append(i)
dfs(pair, n)
pair.pop()
dfs([], len(nums))
return answer
간단하게 라이브러리 사용해서 풀기
import itertools
def permute(self, nums):
return list(itertools.permutations(nums))
itertools 모듈을 쓰면 아주 간단하게 표현 가능하다.
근데 라이브러리 쓰지 말고 직접 풀어보라고 명시하는 곳도 있다고 들어서 일단 나는 잘 사용안하려고 하긴하는데
별도의 제약사항이 없다면 구현의 효율성 및 성능 개선을 위해 사용하는 것이 좋다고 한다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 51. N-Queens with Python 백트래킹 문제 (1) | 2024.01.10 |
---|---|
LeetCode 77. Combinations(조합) with Python (0) | 2024.01.10 |
LeetCode 17. Letter Combinations of a Phone Number with Python (1) | 2024.01.10 |
LeetCode 328. Odd Even Linked List with Python (0) | 2024.01.06 |
LeetCode 21. Merge Two Sorted Lists with Python (0) | 2024.01.06 |