Leetcode dfs Combination SumII

Combination Sum II

Total Accepted: 13710 Total
Submissions: 55908My Submissions

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 positive integers.
  • Elements in a combination (a1, a2,
    … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
    … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and
target 8,

A solution set is:

[1, 7]

[1, 2, 5]

[2, 6]

[1, 1, 6]

题意:给定一组数C和一个数值T,在C中找到所有总和等于T的组合。C中的同一数字最多只能拿一次,找到的组合不能重复。

思路:dfs

第i层的第j个节点有  n - i - j 个选择分支

递归深度:递归到总和大于等于T就可以返回了

复杂度:时间O(n!),空间O(n)

vector<vector<int> > res;
vector<int> _num;
void dfs(int start, int target, vector<int> &path){
	if(target == 0) {res.push_back(path); return;}
	int previous = -1; //这里要加上这个来记录同一层分枝的前一个值,如果当前值跟前一个值一样,就跳过,避免重复
	for(int i = start; i < _num.size(); ++i){
		if(previous == _num[i]) continue;
		if(target < _num[i]) return; //剪枝
		previous = _num[i];
		path.push_back(_num[i]);
		dfs(i + 1, target - _num[i], path);
		path.pop_back();
	}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target){
	_num = num;
	sort(_num.begin(), _num.end());
	vector<int> path;
	dfs(0, target, path);
	return res;
}

时间: 2024-11-09 00:49:52

Leetcode dfs Combination SumII的相关文章

Leetcode dfs Combination Sum

Combination Sum Total Accepted: 17319 Total Submissions: 65259My Submissions 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

Leetcode dfs Path SumII

Path Sum II Total Accepted: 18489 Total Submissions: 68323My Submissions Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4

[leetcode]Combination SumII

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 toT. Each number in C may only be used once in the combination. Note: All numbers (including ta

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

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

Leetcode - Letter Combination Of A Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

[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