leetcode 216 组合总和3
避免顺序不同,同时控制长度与和
class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: def dfs(i,path,target,n,k,result): if target==n and len(path)==k: result.append(path[:]) return elif target==n and len(path)!=k: return elif len(path)>=k: return for j in range(i,10): path.append(j) dfs(j+1,path,target+j,n,k,result) path.pop() result=[] dfs(1,[],0,n,k,result) return result