Combination Sum 系列题解

题目来源:https://leetcode.com/problems/combination-sum/description/


Description

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 target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example

For example, given candidate set [2, 3, 6, 7] and target 7,

A solution set is:

[
[7],
[2, 2, 3]
]

Solution

class Solution {
private:
void backTrack(vector<int>& path, vector<vector<int> >& res,
vector<int>& candidates, int begin, int target) {
if (target == 0) {
res.push_back(path);
} else {
int size = candidates.size();
if (begin >= size)
return;
for (int i = begin; i < size; i++) {
if (candidates[i] <= target) {
path.push_back(candidates[i]);
backTrack(path, res, candidates, i, target - candidates[i]);
path.pop_back();
}
}
}
}
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > res;
vector<int> path;
backTrack(path, res, candidates, 0, target);
return res;
}
};

解题描述

这道题类似与经典的零钱兑换问题,在给定的数组candidates,找出所有和为target的数字组合,选择的数字可以重复但解法不能重复。上面使用的是递归回溯的办法,类似DFS,不难理解,下面再多给出使用迭代DP的解法:

class Solution {
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<vector<int> > > dp(target + 1, vector<vector<int> >());
dp[0].push_back(vector<int>());
for (auto candidate : candidates) {
for (int j = candidate; j <= target; j++) {
if (!dp[j - candidate].empty()) {
auto paths = dp[j - candidate];
for (auto& path : paths)
path.push_back(candidate);
dp[j].insert(dp[j].end(), paths.begin(), paths.end());
}
}
}
return dp[target];
}
};

进阶(Combination Sum II)

进阶版本在第一版的基础上对增加了更多的条件:给出的candidates数组的元素是可以重复的,所以最大问题就是去重,下面给出递归DFS的做法:

class Solution {
private:
int size;
void dfs(vector<vector<int>>& res, vector<int>& path,
vector<int>& candidates, int begin, int target) {
if (target == 0) {
res.push_back(path);
} else if (begin < size) {
for (int i = begin; i < size; i++) {
if (candidates[i] <= target) {
if (i > 0 && i > begin &&
candidates[i] == candidates[i - 1])
continue; //去重的关键过滤步骤:跳过所有相同的元素
path.push_back(candidates[i]);
dfs(res, path, candidates,
i + 1, target - candidates[i]);
path.pop_back();
} else {
return;
}
}
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
this->size = candidates.size();
vector<vector<int>> res;
vector<int> path;
dfs(res, path, candidates, 0, target);
return res;
}
};

[Leetcode] Combination Sum 系列的更多相关文章

  1. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

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

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

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

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

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

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

  5. [LeetCode] Combination Sum 组合之和

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

  6. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  7. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  8. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  9. LeetCode:Combination Sum I II

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

随机推荐

  1. 第142天:Size Marks下载安装和使用方法

    Size Marks下载安装使用方法 一.下载安装 1.下载Size marks:链接: https://pan.baidu.com/s/1breyMf1 密码: fjsn 2. 复制 Size Ma ...

  2. HUST1017-Exact Cover

    给出一个\(n\times m\)的01矩阵,每行最多有\(c\)个1,求一个精确覆盖,即选出一些行使得每列有有且仅有一个1.输出方案. 分析 被这个题坑到了啊!!第一次上HUSTOJ做题,不知道没有 ...

  3. [poi2011]bzoj 2277 —— strongbox·[洛谷3518]

    ·问题描述· 有一个密码箱,0到n-1中的某些数是它的密码.且满足:如果a和b都是它的密码,那么(a+b)%n也是它的密码.某人试了k次密码,前k-1次都失败了,最后一次成功. 问:该密码箱最多有多少 ...

  4. 连接Mysql数据库

    JDBC连接数据库 创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.la ...

  5. Codeforces 744C. Hongcow Buys a Deck of Cards(状压DP)

    这题的难点在于状态的设计 首先显然是个状压,需要一维表示卡的状态,另一维如果设计成天数,难以知道当前的钱数,没法确定是否能够购买新的卡,如果设计成钱数,会发现状态数过多,空间与时间都无法承受.但是可以 ...

  6. 解题:USACO06DEC Milk Patterns

    题面 初见SA 用了一个常见的按$height$分组的操作:二分答案,然后按$height$分组,遇到一个$height$小于$mid$的就丢进下一组并更新答案,如果最多的那组不少于$k$个说明可行 ...

  7. mysql四-1:单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

  8. Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. jbpm3.2创建数据库

    因为jbpm3.2的sql脚本有问题,所以我们通过查询来执行是有问题的,所以这里我们通过java代码来执行,这个是没有问题的. 参考博文: http://blog.csdn.net/sz_bdqn/a ...

  10. 我学习的第一个uiautomator从创建到运行结束

    一.新建自动化脚本     1.新建java工程包              [file]----[new]----[Java Project]    新建工程                  [右 ...