边工作边刷题:70天一遍leetcode: day 23-2

Combinations

题目很简单,但是有出错点:loop不是把start append,而是循环变量i

class Solution(object):
    def combine(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[List[int]]
        """
        def combineRec(start, n, k, res, solutions):
            if len(res)==k:
                resCp = list(res)
                solutions.append(resCp)
                return
            
            if start>n:
                return
            
            for i in range(start, n+1):
                res.append(i)
                combineRec(i+1, n, k, res, solutions)
                res.pop()
        solutions = []
        res = []
        combineRec(1, n, k, res, solutions)
        return solutions
                
posted @ 2016-04-26 10:20  absolute100  阅读(81)  评论(0编辑  收藏  举报