Combination Sum III
Problem: Combination Sum III
Solution of this problem is quite similar to Combination Sum so I won't explain any more.
Code in Python:
class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans = []
def helper(pos, sum, current):
if len(current) < k:
for i in xrange(pos, len(nums)):
if sum + nums[i] == n and len(current) == k-1: ans.append(current+[nums[i]])
elif sum + nums[i] < n - nums[i]: helper(i+1, sum+nums[i], current+[nums[i]])
helper(0, 0, [])
return ans