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 (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 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3]

 /** 整理的提交代码
* 处理复杂度为
* 主要思路:与Combination Sum不同之处在与每轮每个候选数字只能取一次,所以下次递归时考虑的当前元素的之后的元素
* 所以这次在index中存放的下标是当前元素的下一个位置,以便递归时直接跳过上次考察过的元素,避免重复考察。
* 但是在记录结果时需要回复记录在index中的下标。
* 回溯法http://www.leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html
* 提交结果:
* (Judge Small)
* Run Status: Accepted!
* Program Runtime: 4 milli secs (基本在几毫秒)
* Progress: 22/22 test cases passed.
* (Judge Large)
* Run Status: Accepted!
* Program Runtime: 144 milli secs (基本稳定在一百四十几毫秒左右)
* Progress: 172/172 test cases passed.
*/
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std; class Solution {
private:
const int index_count;
vector<vector<int> > results;
public:
Solution() : index_count() {};
// 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 = ; i <= n; ++i)
{
result.push_back(candidates[index[i]-]); // 这里需要减一,因为下面每次记录索引时加了1
}
results.push_back(result);
return; // 此处可以不加,如果不加return由于都是正整数,到下面的计算时会多进行一次无用的递归。
} // 深度搜索,为了避免重复,每次从当前候选项索引到结尾,上面的i=index[n]可以看出
for (int i = index[n]; i < candidates.size(); ++i)
{
index[n+] = i+; // 记录当前考察的候选项索引并加一,下次考察是跳过上次考察过的元素,每轮每个元素值考察一次
backtrace(target, sum+candidates[i], candidates, index, n+);
}
}
vector<vector<int> > combinationSum2(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, , sizeof(int)*index_count); results.clear(); // 提交到leetcode的测试系统上必须添加,它应该是使用一个对象测试所有测试用例。
backtrace(target, , candidates, index, ); delete[] index; // 去重
vector<vector<int> >::iterator end = results.end();
sort(results.begin(), end, less<vector<int> >());
vector<vector<int> >::iterator new_end = unique(results.begin(), results.end());
results.erase(new_end, end); 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.combinationSum2(candidates, target); for (size_t i = ; i < result.size(); ++i)
{
for (size_t j = ; j < result[i].size(); ++j)
{
cout << result[i][j] << ' ';
}
cout << endl;
}
cout << endl; return ;
}
 

 

【转载】Combination Sum的更多相关文章

  1. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  2. DFS-leetcode Combination Sum I/I I

    深度优先搜索(DFS)它是一个搜索算法.第一次接触DFS它应该是一个二进制树的遍历内部,二叉树预订.序和后序实际上属于深度遍历-first.在本质上,深度优先搜索,遍历中则看到了更纯正的深度优先搜索算 ...

  3. Leetcode dfs Combination Sum

    Combination Sum Total Accepted: 17319 Total Submissions: 65259My Submissions Given a set of candidat ...

  4. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. 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 ...

  9. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

随机推荐

  1. Redis源码解析:21sentinel(二)定期发送消息、检测主观下线

    六:定时发送消息 哨兵每隔一段时间,会向其所监控的所有实例发送一些命令,用于获取这些实例的状态.这些命令包括:"PING"."INFO"和"PUBLI ...

  2. HDU5412 CRB and Queries 整体二分

    传送门 刚觉得最近写代码比较顺畅没什么Bug,cdq分治真是我的一个噩梦.. 整体二分模板题,带修改的区间第k小. vjudge不知抽什么风,用不了,hdu忘了密码了一直在那里各种试,难受.. 写得比 ...

  3. echarts--例子

    echarts使用例子: <script type="text/javascript"> option = { title : {'x': 'center','y':' ...

  4. 解决springmvc 中文post请求乱码的过滤器配置

    在web.xml中添加如下配置 <!-- 过滤器 解决post乱码 --> <filter> <filter-name>characterEncodingFilte ...

  5. 2019.10.25 csp-s模拟测试86 反思总结

    继续存档 早上来补了一下昨天的题,不过肯定这两天的没法完全补起来 T1: 经典思路:关于位运算的题讨论每一位的贡献 #include<iostream> #include<cstdi ...

  6. day18 12.丢失更新介绍与悲观锁

    共享锁在一条记录上是可以加多个的,共享嘛.排它锁的意思是指这条记录上如果有任何其他的锁我排它锁是加不上的,有了排它锁其他锁也是加不上的,唯一的.比如说现在我的记录上没锁,加了排它锁其他人使用不了,我这 ...

  7. linux常用目录介绍

    bin:全称binary,含义是二进制,该目录中存放的都是一些二进制文件,这些文件都是可以被运行的. dev:该目录主要存放的是外接设备,例如:光盘等.注意:在其中的设备是不能被直接使用的,需要挂载( ...

  8. 微信小程序之组件开发中的基础知识

    跟着视频开始小程序的项目的开发,视频中这个小程序已经上线了,可以很好的看着小程序的界面进行开发,昨天看了一下具体的需求,觉得真的细节好多啊,而且其中设计的组件的思想也是很好的,能够很好的实现代码的复用 ...

  9. SQL竖表转横表Json数据

    1.数据准备 create  table  Vertical(  Id  int ,  ProjectName varchar(20),  ProjectValue int ) insert into ...

  10. 重磅!阿里云Promtheus 正式免费公测

    每日头条 重磅!容器集群监控利器 阿里云Promtheus 正式免费公测 Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.2019 年7月3日,阿里云Promth ...