19.2.3 [LeetCode 39] Combination Sum

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

 1 class Solution {
 2 public:
 3     void getsum(vector<vector<int>>&res,vector<int> ans,vector<int>&candidates, int target, int lastidx) {
 4         if (target == 0) {
 5             res.push_back(ans);
 6             return;
 7         }
 8         if (target < candidates[lastidx])return;
 9         for (int i = lastidx; i < candidates.size(); i++) {
10             if (target < candidates[i])break;
11             ans.push_back(candidates[i]);
12             getsum(res, ans, candidates, target - candidates[i], i);
13             ans.pop_back();
14         }
15     }
16     vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
17         sort(candidates.begin(), candidates.end());
18         vector<vector<int>>res;
19         vector<int>ans;
20         getsum(res, ans, candidates, target, 0);
21         return res;
22     }
23 };

感觉可以用dp,不知道会不会快

原文地址:https://www.cnblogs.com/yalphait/p/10350697.html

时间: 2024-10-10 04:51:10

19.2.3 [LeetCode 39] Combination Sum的相关文章

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

LeetCode 39. Combination Sum (组合的和)

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (in

19.2.3 [LeetCode 40] Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All nu

Java [Leetcode 39]Combination Sum

题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode 39 Combination Sum(满足求和等于target的所有组合)

题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所有满足求和等于target的数字组合 遍历所有的数组中元素,然后对target进行更新,将该元素添加到tempList中,直到remain等于0时达到条件,可以将该tempList添加到list中 注意:每个元素可以使用多次,因此每次的遍历都要从上次的那个下标开始. 当target更新到小于0的时候,返

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

Java for LeetCode 216 Combination Sum III

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. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode: 题解] Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will