ans
to store the final combinations.backtrack
that takes the current combination curr
and the next starting number firstNum
.curr
is k
, add a copy of curr
to ans
and return.k
(need = k - curr.length
).[firstNum, n]
(remain = n - firstNum + 1
).curr
(available = remain - need
).[firstNum, firstNum + available]
and for each number:
a. Add the number to curr
.
b. Recursively call backtrack
with the updated curr
and num + 1
as the new firstNum
.
c. Remove the last number from curr
to backtrack.backtrack
with an empty curr
and firstNum = 1
.ans
.