LeetCode 40. 组合总和 II(Combination Sum II)

题目描述

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

candidates 中的每个数字在每个组合中只能使用一次。

说明:

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

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

示例 2:

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

解题思路

利用回溯思想,首先将数组从小到大排序,然后从第一个数开始,若当前数字和小于目标,则从当前索引的数开始向后依次加入到当前vector中,并更新当前索引和数字和,再从下一个索引重复此动作,直到遇到数字和等于目标。

代码

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

原文地址:https://www.cnblogs.com/wmx24/p/9400520.html

时间: 2024-10-09 09:29:42

LeetCode 40. 组合总和 II(Combination Sum II)的相关文章

LeetCode 39. 组合总和(Combination Sum)

题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ] 示例 2: 输入: candidates = [2,3,5], t

[Leetcode 40]组合数和II 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: A

leetcode 40. 组合总和 II (python)

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数.解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]示例 2: 输入: candidat

[leetcode 40. 组合总和 II] 不排序使用哈希表+dfs递归 vs 排序栈+回溯

题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] 示例 2: 输入

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

Combination Sum II 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 combination. Note: All numbers (including t

[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

LeetCode: Combination Sum II [039]

[题目] 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 combination. Note: All numbers (including target) will be

[LeetCode][JavaScript]Combination Sum II

Combination Sum II 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 combination. Note: All numbers (including t

【leetcode】Combination Sum II

Combination Sum II 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 combination. Note: All numbers (including t