GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 组合总和-39 · Issue #72 · sl1673495/leetcode-javascript

组合总和-39 #72

New issue
Copy link
New issue
Copy link
Open
Open
组合总和-39#72
Copy link
Labels
待复习看题解或者做出来很艰难的,需要回顾。看题解或者做出来很艰难的,需要回顾。递归与回溯

Description

@sl1673495
sl1673495
opened on Jun 12, 2020
Issue body actions

给定一个无重复元素的数组  candidates  和一个目标数  target ,找出  candidates  中所有可以使数字和为  target  的组合。

candidates  中的数字可以无限制重复被选取。

说明:

所有数字(包括  target)都是正整数。
解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
  [7],
  [2,2,3]
]
示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combination-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这题乍一看很难,因为有重复元素,相对于之前的几道题来说引入了新的概念。

但是其实仔细想一下,之前的递归,我们对于 helper 递归函数每次递归都会把 start 起点 +1,如果我们每次递归不去 +1,而是把 start也考虑在内的话,是不是就可以把重复元素的情况也考虑进去了呢?

/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
let combinationSum = function (candidates, target) {
  let res = []

  let helper = (start, prevSum, prevArr) => {
    // 由于全是正整数 所以一旦和大于目标值了 直接结束本次递归即可。
    if (prevSum > target) {
      return
    }
    // 目标值达成
    if (prevSum === target) {
      res.push(prevArr)
      return
    }

    for (let i = start; i < candidates.length; i++) {
      // 这里还是继续从start本身开始 因为多个重复值是允许的
      let cur = candidates[i]
      let sum = prevSum + cur
      let arr = prevArr.concat(cur)
      helper(i, sum, arr)
    }
  }

  helper(0, 0, [])

  return res
}

Activity

  1. sl1673495
    added
    待复习看题解或者做出来很艰难的,需要回顾。看题解或者做出来很艰难的,需要回顾。
    递归与回溯
    on Jun 12, 2020
  2. Repository owner deleted a comment from jinfang12345 on Jun 24, 2020
  3. daomingQian commented on Oct 21, 2021

    @daomingQian
    daomingQian
    on Oct 21, 2021
    More actions
    var combinationSum = function(candidates, target) {
        let res = []
        let len = candidates.length
    
        function sum(arr) {
            return arr.reduce((pre, cur) => pre + cur, 0)
        }
    
        function tfs(start, path) {
            if (sum(path) === target) {
                res.push([...path])
                return
            }
            if (sum(path) > target) {
                return
            }
    
            for (let i = start; i < len; i++) {
                path.push(candidates[i])
                tfs(i, path)
                path.pop()
            }
        }
        tfs(0, [])
        return res
    };
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    待复习看题解或者做出来很艰难的,需要回顾。看题解或者做出来很艰难的,需要回顾。递归与回溯

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.