class Solution:
# @return a list of lists of integers
# 9:14
def __init__(self):
self.output = []
def combine(self, n, k, pos=0, temp=None):
temp = temp or []
if len(temp) == k:
self.output.append(temp[:])
return
for i in range(pos, n):
temp.append(i+1)
self.combine(n, k, i+1, temp)
temp.pop()
return self.output
Python recursive solution


@dharaB self.output.append(temp[:]) will make a shallow copy of array temp, if you don't specify this, and pass in just temp, then you will change the reference of temp, and all of your return value will be modified.

@yu_zhoujr said in Python recursive solution:
@dharaB self.output.append(temp[:]) will make a shallow copy of array temp, if you don't specify this, and pass in just temp, then you will change the reference of temp, and all of your return value will be modified.
its a deep copy, try it youself