Combination Sum 和Combination Sum II

这两道题的基本思路和combination那一题是一致的,也是分治的方法。

其中combination Sum复杂一点,因为每个数可能用多次。仔细分析下,本质上也是一样的。原来是每个数仅两种可能。现在每个数有k +1中可能,k = target / i。

所以就是把简单的if else 分支变成for循环:

vector<vector<int> > combHelper(vector<int>::iterator first,
						vector<int>::iterator last, int target){
	vector<vector<int>> result;
	if (target <= 0 || last == first) return result;
	if (*(last - 1) == target){
		vector<int> vi;
		vi.push_back(target);
		result.push_back(vi);
		auto r2 = combHelper(first, last - 1, target);
		for (auto it = r2.begin(); it != r2.end(); it++)
			result.push_back(*it);
	}
	else if (*(last - 1) < target){
		auto r1 = combHelper(first, last - 1, target - *(last - 1));
		for (auto it = r1.begin(); it != r1.end(); it++){
			it->push_back(*(last - 1));
			result.push_back(*it);
		}
		auto r2 = combHelper(first, last - 1, target);
		for (auto it = r2.begin(); it != r2.end(); it++)
			result.push_back(*it);
	}
	else {
		auto r2 = combHelper(first, last - 1, target);
		for (auto it = r2.begin(); it != r2.end(); it++)
			result.push_back(*it);
	}
	return result;
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
	sort(num.begin(), num.end());
	auto result = combHelper(num.begin(), num.end(), target);
	if (!result.empty()){
		sort(result.begin(), result.end());
		auto it = unique(result.begin(), result.end());
		result.erase(it, result.end());
	}
	return result;
}

  

时间: 2024-08-04 16:11:05

Combination Sum 和Combination Sum II的相关文章

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a>>b; cout<<"a+b="<<sum<<endl; return 0;} //原因sum=a+b;此语句位置不对,变量a,b在没有赋值时就被相加,超出int最大值范围.只能得到最大值65538 #include <iostream>

leetcode-combination sum and combination sum II

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 (includ

UVALive8518 Sum of xor sum

题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回答这个区间内的子序列的所有子序列的异或和之和. $1 \le N,Q \le 100000$ $0 \le A[i] \le 1000000$ 知识点: 前缀和 解题思路: 将序列中的每一个数转成二进制(不超过 $20$ 位),逐位考虑. 根据序列中的数字用二进制表示时在该位上为 $1$ 或 $0$

Combination Sum I and II

比较典型的helper的题目,现在我还搞不太清楚dfs之类的,只能用helper来统称了,你明白那个调调就行 public class Solution { public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { ArrayList<ArrayList<Integer>> res =new ArrayList<ArrayList<Int

[leetcode] Combination Sum and Combination SumII

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

112. Path Sum 113. Path Sum II

回溯法的典型 1.Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return t

two sum, three sum和four sum问题

1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash表,然后只需要外层一层循环就可以了,时间复杂度为n 2. three sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的三个数 简单做法:三层for循环,时间复杂度为n^3 升级版: 如果看做是two sum升级,可以先从小到大排序,时间复杂度nlog

[Leetcode] 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 target) will be posi