def combine(self, n, k): res =  if k == 1: for i in xrange(1,n+1): res.append([i]) return res res = self.combine(n, k-1) newres =  for list in res: for i in xrange(list[-1]+1, n+1): newres.append(list+[i]) return newres
This is my python code using recursion. It's quite simple.
The key point is to understand how to get the outcome from the last result.
For example, from k to k+1, that is to say every list in k th result should be added on element.
The added element should be smaller than or equal to n . If n = 4, and we want to get the outcome when k = 3. Suppose the list [1,2] is one of the list in the result of k = 2, so this list can be added element 3 and element 4. However, for the list[1,4], we can not add any element for this list.