77. Combinations
2021. 10. 20. 01:20ㆍLeetcode
Description:
Given two integers n and k, return all possible combinations of k numbers out of the range [1, n].
You may return the answer in any order.
'''
[1] naive solution)
def recur(ans,list,low,k,n):
if len(list)==k:
ans.append(list.copy())
return
for i in range(low,n+1):
list.append(i)
recur(ans,list,i+1,k,n)
list.pop()
ans=[]
recur(ans,[],1,k,n)
return ans
[2] using python library)
from itetools import combinations
return combinations(range(1,n+1),k)
'''
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
return combinations(range(1,n+1),k)
'Leetcode' 카테고리의 다른 글
151. Reverse Words in a String (0) | 2021.10.20 |
---|---|
40. Combination Sum II (0) | 2021.10.20 |
496. Next Greater Element I (0) | 2021.10.20 |
661. Image Smoother (0) | 2021.10.17 |
73. Set Matrix Zeroes (0) | 2021.10.17 |