CMN=M!N!(MN)!,(M=4, N=2)C_M^N =\frac{M!}{N!(M-N)!}, \text{(M=4, N=2)}

77. Combinations[E]

https://leetcode.com/problems/combinations/

Description

Given two integers n and k, return all possible combinations of k numbers out of 1 … n.

Example:

1
2
3
4
5
6
7
8
9
10
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

Solution

1
2
3
4
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
import itertools as it
return [list(x) for x in list(it.combinations([i for i in range(1,n+1)], k))]