题目
思路
经典回溯法
代码
class Solution(object):
def __init__(self):
self.result = []
self.path = []
def combinationSum3(self, k, n):
self.find(1, k, n)
return self.result
def find(self, index, k, n):
if len(self.path)==k: # 递归终止条件
if sum(self.path) == n:
self.result.append(self.path[:]) # 符合题目的答案之一
return 0
for i in range(index, 10):
self.path.append(i)
self.find(i+1,k,n) # 递归
self.path.pop() # 回溯