• 43.2%

https://leetcode.com/problems/combination-sum-iii/description/

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Use backtrack c++ solution, easy to understand.

i<=10-k && i<=target

https://discuss.leetcode.com/topic/14641/my-c-solution-backtracking

My C++ solution, backtracking.

https://discuss.leetcode.com/topic/25351/use-backtrack-c-solution-easy-to-understand

Use backtrack c++ solution, easy to understand.

52ms, 43.32%, June.18th, 2016

https://leetcode.com/discuss/38132/concise-python-solution-using-dfs

Concise python solution using DFS

https://discuss.leetcode.com/topic/14702/clean-1-6-7-liners-ac

Clean 1/6/7-liners (AC)

Batteries Included

AC in 44ms

First the obligatory “use the darn library” solution. Create all k-combinations of digits and keep those with sum n:

Recursive

AC in 48 ms

But it’s more interesting to do it on your own. Here I use a recursive helper function getting the same k and n as the main function, and an additional cap under which all the numbers have to be:

Iterative

AC in 56 ms

And an iterative version doing pretty much the same thing, except this time I prepend elements on the left, and use the first element of a partial combination as the cap.

Reduce

AC in 44 ms

And here’s a “one-liner” version of the iterative solution using reduce instead of the loop:

I note that all these solutions also correctly solve the cases with k=0 and/or n=0 (but leetcode sadly doesn’t test those).

https://discuss.leetcode.com/topic/19100/easy-to-understand-python-solution-backtracking

Easy to understand Python solution (backtracking).