Given a set of candidate numbers (C) (without duplicates) 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 the target) will be positive integers.
  • 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]
]

本题是 backtrack(回溯法) 的经典应用.通过本题,仔细观察 backtrack 基本框架,并感受其应用.

另外, subset 的题目也用 backtrack 方法.

人家想法,自个代码:

  • cpp 副产品: v.pop_back(); 弹出队尾元素.
  • 要注意的是: 下面代码中 backtrack 方法中的 res, temp, A, 尤其是 temp, 他们都分别指向对应的一个对象,无论 backtrack 方法被嵌套地调用多少次!
// backtrace method
vector<vector<int>> combinationSum(vector<int>& A, int ta) {
sort(A.begin(), A.end());
vector < vector<int> > res;
vector<int> temp;
backtrack(res, temp, A, ta, 0);
return res;
} void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
int remain, int start) {
if (remain < 0)
return;
else if (remain == 0) {
res.push_back(temp);
return;
} else {
for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]); // not i + 1, because A[i] might be reused.
backtrack(res, temp, A, remain - A[i], i); temp.pop_back();
}
return;
}
}

39. Combination Sum(medium, backtrack 的经典应用, 重要)的更多相关文章

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

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

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

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

  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. 39. Combination Sum (Back-Track)

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

随机推荐

  1. 新概念英语(1-111)The most expensive model

    Lesson 111 The most expensive model 最昂贵的型号 Listen to the tape then answer this question. Can Mr. Fri ...

  2. linux下的Shell编程(5)循环

    Shell Script中的循环有下面几种格式: while [ cond1 ] && { || } [ cond2 ] -; do - done for var in -; do - ...

  3. OAuth2.0学习(1-4)授权方式1-授权码模式(authorization code)

    参与者列表: (1) Third-party application:第三方应用程序,又称客户端(client),如:"云冲印".社交应用. (2)HTTP service:HTT ...

  4. Python/Django-Web原理(一)

    Python/Django-Web原理(一) websocket webSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML规范中被引用为TCP连接,作为基于TCP的套接字AP ...

  5. API验证及AES加密

    API验证 API验证: a. 发令牌: 静态 PS: 隐患 key被别人获取 b. 动态令牌 PS: (问题越严重)用户生成的每个令牌被黑客获取到,都会破解 c. 高级版本 PS: 黑客网速快,会窃 ...

  6. SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...

  7. ·c#之Thread实现暂停继续(转)

    暂停与继续实现,可以使用Thread.Suspend和Thread.Resume而这两个方法,在VS2010里提示已经过时,不建议使用,在网上查阅了一些资料,发现有个事件通知的方法很好,事件通知的大致 ...

  8. js高阶函数应用—函数柯里化和反柯里化

    在Lambda演算(一套数理逻辑的形式系统,具体我也没深入研究过)中有个小技巧:假如一个函数只能收一个参数,那么这个函数怎么实现加法呢,因为高阶函数是可以当参数传递和返回值的,所以问题就简化为:写一个 ...

  9. kafka知识体系-kafka设计和原理分析-kafka文件存储机制

    kafka文件存储机制 topic中partition存储分布 假设实验环境中Kafka集群只有一个broker,xxx/message-folder为数据文件存储根目录,在Kafka broker中 ...

  10. phpcmsV9.5.8 后台拿shell

    参考url:https://xianzhi.aliyun.com/forum/read/1507.html poc:index.php??m=content&c=content&a=p ...