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

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

/** 整理的提交代码
 * 处理复杂度为
 * 主要思路:回溯法http://www.leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html
 * 提交结果: 
 * (Judge Small) 
 * Run Status: Accepted! 
 * Program Runtime: 8 milli secs (基本在几毫秒)
 * Progress: 17/17 test cases passed. 
 * (Judge Large) 
 * Run Status: Accepted!
 * Program Runtime: 148 milli secs	(基本稳定在一百四十几毫秒左右)
 * Progress: 168/168 test cases passed.
 */
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

class Solution {
private:
	const int index_count;
	vector<vector<int> > results;
public:
	Solution() : index_count(10000) {};
	// index记录当前找到的候选数字,n表示当前正在找第几个,n是index的下标不是candidates的下标
	void backtrace(int target, int sum, vector<int> &candidates, int index[], int n)
	{
		if (sum > target)
		{
			return;	// 回溯
		}
		if (sum == target)
		{
			vector<int> result;
			for (int i = 1; i <= n; ++i)
			{
				result.push_back(candidates[index[i]]);	
			}
			results.push_back(result);
			return;	// 此处可以不加,如果不加return由于都是正整数,到下面的计算时会多进行一次无用的递归。
		}

		// 深度搜索,为了避免重复,每次从当前候选项索引到结尾,上面的i=index[n]可以看出
		for (int i = index[n]; i < candidates.size(); ++i)
		{
			index[n+1] = i;	// 记录当前考察的候选项索引
			backtrace(target, sum+candidates[i], candidates, index, n+1);
		}
	}
	vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		sort(candidates.begin(), candidates.end());

		int *index = new int[index_count];
		memset(index, 0, sizeof(int)*index_count);
		
		results.clear();	// 提交到leetcode的测试系统上必须添加,它应该是使用一个对象测试所有测试用例。
		backtrace(target, 0, candidates, index, 0);

		delete[] index;

		return results;
	}
};

int main()
{
	vector<int> candidates;
	int number;
	cout << "input candidates: ";
	while (cin >> number)
	{
		candidates.push_back(number);
	}

	// 清除缓冲区
	cin.sync();
	cin.clear();

	int target;
	cout << "input target: ";
	cin >> target;

	vector<vector<int> > result;
	Solution s;
	result = s.combinationSum(candidates, target);

	for (size_t i = 0; i < result.size(); ++i)
	{
		for (size_t j = 0; j < result[i].size(); ++j)
		{
			cout << result[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值