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. Oracle schema 的含义

    方案(Schema)为数据库对象的集合,为了区分各个集合,我们需要给这个集合起个名字,这些名字就是我们在企业管理器的方案下看到的许多类似用户名的节点,这些类似用户名的节点其实就是一个schema,sc ...

  2. jmeter 兼容bug 记录一笔

    这个问题我也遇到过,然后网上搜到了这篇文章! 先说下问题: 我在做性能测试时,使用JMeter搞了100个并发,以100TPS的压力压测十分钟,但压力一直出现波动,而且出现波动时JMeter十分卡,如 ...

  3. 转发---[沧海拾遗]java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch CountDownLatch是一个计 ...

  4. BZOJ4883 棋盘上的守卫(环套树+最小生成树)

    容易想到网络流之类的东西,虽然范围看起来不太可做,不过这提供了一种想法,即将行列分别看做点.那么我们需要找一种连n+m条边的方案,使得可以从每条边中选一个点以覆盖所有点.显然每个点至少要连一条边.于是 ...

  5. C++解析(26):函数模板与类模板

    0.目录 1.函数模板 1.1 函数模板与泛型编程 1.2 多参数函数模板 1.3 函数重载遇上函数模板 2.类模板 2.1 类模板 2.2 多参数类模板与特化 2.3 特化的深度分析 3.小结 1. ...

  6. C++解析(22):父子间的冲突

    0.目录 1.同名覆盖 2.赋值兼容 3.函数重写遇上赋值兼容 4.小结 1.同名覆盖 子类中是否可以定义父类中的同名成员?如果可以,如何区分?如果不可以,为什么? 父子间的冲突: 子类可以定义父类中 ...

  7. [BZOJ4870][Shoi2017]组合数问题 dp+矩阵乘

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MB Description Input 第一行有四个整数 n, p, k, r ...

  8. QT模态对话框及非模态对话框

    QT模态对话框及非模态对话框 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对 ...

  9. 获取AD域中SYSVOL和组策略首选项中的密码

    这种方法是最简单的,因为不需要特殊的“黑客”工具.所有的攻击必须做的是打开Windows资源管理器,并搜索域名为SYSVOL DFS共享的XML文件.在大多数情况下,以下XML文件将包含凭据:grou ...

  10. C++堆和栈详解(转)

    一.预备知识—程序的内存分配    一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等.其    操 ...