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. Zookeeper分布式服务协调组件

    1.简介 Zookeeper是一个分布式服务协调组件,是Hadoop.Hbase.Kafka的重要组件,它是一个为分布式应用提供一致性服务的组件.   Zookeeper的目标就是封装好复杂易出错的服 ...

  2. html5的八大特性

    HTML5是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML [1](标准通用标记语言下的一个应用)标准版本:现在仍处于发展阶段,但大部分浏览器已经支持某些 H ...

  3. CSS 选择器简介

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 选择器权重 如果以4位数表示选择符权重,那么: 元素选择器的权重是1: id 选择器的权重为100: cl ...

  4. Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】

    原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...

  5. 一.Kylin的伪分布式安装

    一.伪分布式安装kylin 2018年4月15日 15:06 安装需要的环境 1. hadoop集群环境:由于安装的是CDH5.14.0的版本,所以相关组件都是跟5.14.0相关 2. spark采用 ...

  6. HtmlUnit入门一

    htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容. 项目可以模拟浏览器运行,被誉为java浏览器的开源实现.是一个没有界面的浏览器. 采 ...

  7. Mac下安装oh-my-zsh

    Mac下自带的终端并不好用,当你打开终端的时候是一个白花花的窗口,其实Mac自带几种shell,默认使用的是bash,可以通过 cat /etc/shells 查看几种shell bin/bash / ...

  8. HDU3311Dig The Wells

    给定N个寺庙,和M个另外的地方. 然后给定点权,表示在这个点挖水井需要的代价. 再给定边权,为建造无向边i,j的代价. 然后求怎样弄最小的代价使得前N个点,就是寺庙都能从挖的井里得到水. 输入输出格式 ...

  9. bzoj 1558: [JSOI2009]等差数列

    Description Solution 把原数组变为差分数组,然后剩下的就十分显然了 区间查询用线段树维护 修改操作就是区间加法和两个单点修改 一个等差数列实际上就是 开头一个数字+数值相等的一段 ...

  10. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...