代码随想录训练营第III期--024--python

树模型终于结束了,奥利给!!!

# 代码随想录训练营第III期--024--python

# 回溯的模板
'''
if stop condition:
    save result
    return 
for ():
    operate
    backtracking()
    deoperate
'''
# 77
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:

        res = []
        def backtracking(temp,i):
            if len(temp) == k:
                res.append(temp)
                return 
            for i in range(i,n+1):
                if i not in temp:
                    backtracking(temp+[i],i)
        backtracking([],1)

        return res 

你可能感兴趣的:(代码随想录,python,算法,leetcode)