Skip to content

Latest commit

 

History

History
37 lines (32 loc) · 775 Bytes

组合总和.md

File metadata and controls

37 lines (32 loc) · 775 Bytes

https://leetcode-cn.com/problems/combination-sum/

代码

/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function(candidates, target) {
  let res = []
  let len = candidates.length
  const defs = (path ,start, total) => {
      for (let i = start; i < len; i++) {
          let num = candidates[i]
          if (total < target) {
            path.push(num)
            defs(path, i, total + num)
            path.pop()
          }
          if (total === target) {
            res.push(path.slice())
            break
          }
          if (total > target) {
            continue
          } 
      }
  
  }
  defs([] ,0,0)
  return res
};