Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class Solution:
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
res=[]
def dfs(start,temp,count):
if count==k:
res.append(temp)
return
for i in range(start,n+1):
dfs(i+1,temp+[i],count+1) # dfs(start,temp+[i])这是错的啊,用i+1,就确定了后面加的数肯定比前面大啊
dfs(1,[],0)
return res