首页 > 学院 > 开发设计 > 正文

leecode 解题总结:40 Combination Sum II

2019-11-10 18:41:44
字体:
来源:转载
供稿:网友
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <algorithm>using namespace std;/*问题: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.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]]分析:此题在递归求解的过程中必须确保每个数字出现的次数不能超过实际出现的次数只需要修改for循环中i * candidate <= target中的条件为 i <= numToTimes[i] && i * candidate <= target输入:7(数组元素个数) 8(目标值)10 1 2 7 6 1 5输出:1 7,1 2 5,2 6,1 1 6关键:1 用回溯来做,如果目标值为0,就将结果压入结果集。回溯适用于求解含有多个结果的集合回溯基本格式:尝试某一数值递归清空尝试的数值与可重复累加和的区别在于:可重复累加和一直尝试当前值,直到目标值 < 当前候选值,则选取下一个候选值。  如果目标值>0,继续尝试下一个值,尝试过后,弹出之前压入的值	//如果目标变成0,说明得到结果,将结果加入结果集	if(0 == target)	{		results.push_back(path);		return;	}			//此种情况不可能	if(target < 0)	{		return;	}	int size = candidates.size();	for(int i = cur ; i < size ; i++)	{		//防止添加重复元素,如果当前元素和之前元素重复,就跳过		if(i > cur && candidates.at(i) == candidates.at(i-1))		{			continue;		}		path.push_back(candidates.at(i));		//尝试下一个候选值		combineSum(candidates , target - candidates.at(i) , i + 1 , path , results);		//回溯		path.pop_back();	}2 求组合值为sum的解法 void combinationSum(std::vector<int> &candidates, int target, std::vector<std::vector<int> > &res, std::vector<int> &combination, int begin) {		if  (!target) {			res.push_back(combination);			return;		}        for (int i = begin; i != candidates.size() && target >= candidates[i]; ++i) {            combination.push_back(candidates[i]);            combinationSum(candidates, target - candidates[i], res, combination, i);            combination.pop_back();        }	}*/class Solution {public:	//尝试用摆放来做,问题转化为递归问题	void combineSum(vector<int>& candidates, int target , int cur , vector<int> path , vector< vector<int> >& results)	{		vector< vector<int> > totalResults;		if(candidates.empty())		{			return ;		}		//如果目标变成0,说明得到结果,将结果加入结果集		if(0 == target)		{			results.push_back(path);			return;		}				//此种情况不可能		if(target < 0)		{			return;		}		int size = candidates.size();		for(int i = cur ; i < size ; i++)		{			//防止添加重复元素,如果当前元素和之前元素重复,就跳过			if(i > cur && candidates.at(i) == candidates.at(i-1))			{				continue;			}			path.push_back(candidates.at(i));			//尝试下一个候选值			combineSum(candidates , target - candidates.at(i) , i + 1 , path , results);			//回溯			path.pop_back();		}	}    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {		vector<vector<int>> results;		if(candidates.empty())		{			return results;		}        //必须确保候选值从大到小排序		sort(candidates.begin() , candidates.end());		vector<int> path;		combineSum(candidates, target , 0 , path , results);		return results;    }};void PRint(vector<vector<int>>& results){	if(results.empty())	{		cout << "no result" << endl;		return;	}	int size = results.size();	for(int i = 0 ; i < size ; i++)	{		int len = results.at(i).size();		for(int j = 0 ; j < len ; j++)		{			cout << results.at(i).at(j) << " ";		}		cout << ",";	}	cout << endl;}void process(){	int num;	vector<int> nums;	int target;	int value;	Solution solution;	vector< vector<int> > results;	while(cin >> num >> target)	{		nums.clear();		for(int i = 0 ; i < num ; i++)		{			cin >> value;			nums.push_back(value);		}		results = solution.combinationSum2(nums , target);		print(results);	}}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表