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
- 디자인패턴
- 1주일회고
- 빈 조회 2개 이상
- 프로그래머스 이중우선순위큐
- 코딩테스트 준비
- @FeignClient
- 인프콘 2024
- 전략패턴 #StrategyPattern #디자인패턴
- KPT회고
- 단기개발자코스
- 개발자부트캠프추천
- spring batch 5.0
- infcon 2024
- 파이썬
- 항해99
- 개발자 취업
- 99클럽
- 취업리부트코스
- 프로그래머스
- TiL
- JavaScript
- 디자인 패턴
- jwttoken
- 빈 충돌
- 커스텀 헤더
- Spring multimodule
- Python
- jwt
- DesignPattern
- 구글 OAuth login
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 77. Combinations(조합) with Python 본문
https://leetcode.com/problems/combinations/
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
answer = []
def dfs(pair, i, n, k):
if len(pair) == k:
answer.append(pair[:])
return
for j in range(i, n+1):
pair.append(j)
dfs(pair, j+1, n, k)
pair.pop()
dfs([], 1, n, k)
return answer
라이브러리 사용해서 간편하게 나타내기
import itertools
def combine(self, n, k):
return list(itertools.combinations(range(1, n+1), k))
이 또한 itertools 모듈을 사용하면 좀 더 편하고 성능좋게 나타낼 수 있다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 78. Subsets with Python (0) | 2024.01.11 |
---|---|
LeetCode 51. N-Queens with Python 백트래킹 문제 (1) | 2024.01.10 |
LeetCode 46.Permutations(순열) 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 |