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]

思路:每个元素有被选和不选两种情况=>带回溯的递归

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backTracking(candidates,,target);
return result;
} void backTracking(vector<int>& candidates, int depth, int target){
if(target==){
result.push_back(item);
return;
}
if(target< || depth >= candidates.size()) return; item.push_back(candidates[depth] );
backTracking(candidates,depth, target-candidates[depth]);
item.pop_back();
backTracking(candidates,depth+, target);
}
private:
vector<int> item;
vector<vector<int>> result;
};

39. Combination Sum (Back-Track)的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  8. 【LeetCode】39. Combination Sum (2 solutions)

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

  9. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  10. 39. Combination Sum

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

随机推荐

  1. Win7操作系统安装IE10提示“安装前需要更新与安装程序版本”

    安装IE10浏览器时提示错误的 Internet Explorer安装程序版本 故障现象: Win7操作系统在安装IE10浏览器时会弹出对话框,提示错误的Ieternet Explorer 安装程序版 ...

  2. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 01

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2383] 版本控制:https://git.coding.net/liuyy08 ...

  3. URL 生成带文字二维码

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  4. Android GPS GPSBasics project hacking

    一.参考源码: GPS Basic Example - Android Example http://androidexample.com/GPS_Basic__-__Android_Example/ ...

  5. 重新学习之spring第三个程序,整合struts2+spring

    第一步:导入Struts2jar包+springIOC的jar包和Aop的Jar包 第二步:建立applicationContext.xml文件+struts.xml文件+web.xml文件 web. ...

  6. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  7. [LeetCode系列] K节点倒序问题迭代解法

    给定链表和整数k, 使用in-space方法将链表按k个为一组进行倒序, 如果剩余个数不足k个则保留其原始顺序. 如给定1->2->3->4->5, k = 2, 需要返回 2 ...

  8. js setInterval每隔一段时间执行一次

    js setInterval每隔一段时间执行一次setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearI ...

  9. inline修饰虚函数的问题

    虚函数是否可以内联? 一般来说,inline是编译时的行为,虚函数是在程序执行时的行为,因此编译器一般会拒绝对虚函数进行内联!

  10. make 写法练习

    cc=g++ all:signal %:%.o $(cc) -o $< $@ %.cpp:%.o echo se $< $@ $* $^ g++ -c $< $@cl: rm -rf ...