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. Eclipse:Eclipse插件开发全套教程

    分享是美德,作者为Eclipse核心工程师之一,全英文版,有不明白的地方欢迎探讨和咨询. http://www.vogella.com/tutorials/eclipse.html

  2. For循环和闭包问题

    考虑一下以下的代码片段: for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.append ...

  3. git学习记录——远程仓库(说白了就是代码放到githup上)

    远程仓库 现在讲述的这些SVN都已经做到了,并没什么稀奇的地方 所以这节课赘述的是杀手级的东西——远程仓库githup ssh-keygen -t rsa  -C "xxxxxxxxxxx@ ...

  4. beego 批量删除问题

    o := orm.NewOrm()    qs := o.QueryTable(new(ExecutionJobs))    javaTimestamp = 1557738394000    qs = ...

  5. maven安装教程(Idea)

    下载网址:https://maven.apache.or 点击Download 根据需要下载需要的版本 1.解压下载好的安装包放入新创建的maven文件夹中 2.在系统变量中添加环境变量(建议改成M2 ...

  6. GIT生成公钥和私钥

    转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...

  7. shell linux基本命令实例、笔记

    1. 在当前文件夹下.查找20分钟内,被訪问过的文件, 并将文件的详情显示出来: find ./ -name '*.log' -mmin -20 -exec ls -l {} \;   当然,须要指出 ...

  8. Appium_Python_Client介绍

    一.Appium_Python_Client介绍 Appium的实用方法都藏在Client的源码里,对于driver和webelement实例,均有对应的元素查找方法(webelement查找的是下面 ...

  9. HDU5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  10. POJ 1061 扩展欧几里得

    #include<stdio.h> #include<string.h> typedef long long ll; void gcd(ll a,ll b,ll& d, ...