Skip to content

216.组合总和-iii

题解

js
/**
 * @param {number} k
 * @param {number} n
 * @return {number[][]}
 */
var combinationSum3 = function (k, n) {
  const res = [];
  function fn(startIndex, k, path, sum) {
    if (path.length > k) return;
    if (path.length === k) {
      if (sum === n) {
        res.push([...path]);
      }
      return;
    }
    for (let i = startIndex; i <= 9 - (k - path.length) + 1; i++) {
      fn(i + 1, k, path.concat(i), sum + i);
    }
  }
  fn(1, k, [], 0);
  return res;
};
/**
 * @param {number} k
 * @param {number} n
 * @return {number[][]}
 */
var combinationSum3 = function (k, n) {
  const res = [];
  function fn(startIndex, k, path, sum) {
    if (path.length > k) return;
    if (path.length === k) {
      if (sum === n) {
        res.push([...path]);
      }
      return;
    }
    for (let i = startIndex; i <= 9 - (k - path.length) + 1; i++) {
      fn(i + 1, k, path.concat(i), sum + i);
    }
  }
  fn(1, k, [], 0);
  return res;
};