[array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium
descrition
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.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
解析
典型的回溯法求解。代码实现中给出了 3 中回溯的方式,都 accepted。微妙的区别应该是在递归的层数不同。对于 candidates[index] 只有两种情况,即:选择或不选择,值得注意的是如果选择的话可以多次重复选择。(可以使用状态转换图进行抽象更便于理解,重复选择实际上是在 index 状态有环,而不选择则是向 index + 1 状态的迁移)
注意:
- 调用函数前用了一个排序,主要是为了递归时剪枝做准备,数组是递增排序,如果太大则可以停止更深层的递归
- 题目说了所有数都是 positive,这其实也可以作为剪枝的条件
- 题目说数组中不存在 duplicate 元素,如果存在的话还需要跳过重复的元素。
一般的,对于回溯问题,找好递归求解的子结构,记得结束点即出口的检查,避免无限循环。在递归过程中可以思考是否可以进行剪枝。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target){
vector<vector<int> > ans;
vector<int> vecCur;
sort(candidates.begin(), candidates.end());
combinationSumBacktracking0(candidates, 0, target, vecCur, ans);
//combinationSumBacktracking1(candidates, 0, target, vecCur, ans);
//combinationSumBacktracking2(candidates, 0, target, vecCur, ans);
return ans;
}
// candidates in ascending
void combinationSumBacktracking0(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0 && !vecCur.empty()){
ans.push_back(vecCur);
return;
}
// sub-problem, for each element in candidates[index,...,n-1]
// just have two condition: choose or not
for(int i=index; i<candidates.size(); i++){
if(candidates[i] > target) // Note: candidates must in ascending order
break;
// note: not i+1, because the same repeaded number may be chosen from candidates
vecCur.push_back(candidates[i]);
combinationSumBacktracking0(candidates, i, target - candidates[i], vecCur, ans);
vecCur.pop_back();
}
}
void combinationSumBacktracking1(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
}
if(index >= candidates.size())
return;
// choose candidates[index]
// Note: candidates[index] can be choose more than onece
vecCur.push_back(candidates[index]);
combinationSumBacktracking1(candidates, index, target - candidates[index], vecCur, ans);
vecCur.pop_back();
// dosen't choose candidates[index]
combinationSumBacktracking1(candidates, index+1, target, vecCur, ans);
}
void combinationSumBacktracking2(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
}
if(index >= candidates.size())
return;
// choose candidates[index] more than times
int i = 1;
for(; i*candidates[index] <= target; i++){
vecCur.push_back(candidates[index]);
combinationSumBacktracking2(candidates, index+1, target - i*candidates[index], vecCur, ans);
}
for(int j=i-1; j>=1; j--)
vecCur.pop_back();
// don't choose candidates[index]
combinationSumBacktracking2(candidates, index+1, target, vecCur, ans);
}
};
int main()
{
return 0;
}
[array] leetcode - 39. Combination Sum - Medium的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...
- Leetcode 39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- 学习java的优势
学习java之后,到企业的岗位 技术:java软件开发工程师(中初级):技术一般: 高级工程师:技术高等: 技术架构师:技术顶级: 管理:项目经理:产品经理: 质询:质询顾问:销售经理: 学会之后可以 ...
- jq实现全选或者全不选
$("#all").click(function () { if($(this).is(":checked")){ $("input[name='pr ...
- LayoutInflater 三种获得方式
LayoutInflater 作用是从外部加载一个xml布局文件. 获得 LayoutInflater 实例的三种方式: 1.LayoutInflater inflater = getLayoutIn ...
- centos7下部署Django(nginx+uwsgi+python3+django)
系统版本 centos7 python版本 使用官方python3.6.3正式版 django版本 使用本文发布时最新的1.11.7 uwsgi版本 使用本文发布时最新的2.0.15 nginx版本 ...
- 安装memcached
简介 memcached是免费和开放源代码的高性能分布式内存对象缓存系统,旨在通过减轻数据库负载来加速动态Web应用程序.其有以下特点: 基于简单的文本行协议 全部数据按照k/v形式存放在内存中,无持 ...
- JS的数据类型及转换(还是基础的东西)
朋友说我这是再自娱自乐,我只想说,你说的对
- 深入浅出多线程——ReentrantLock (二)
深入浅出多线程——ReentrantLock (一)文章中介绍了该类的基本使用,以及在源码的角度分析lock().unlock()方法.这次打算在此基础上介绍另一个极为重要的方法newConditio ...
- thinkphp5源码解析(1)数据库
前言 tp5的数据库操作全部通过Db类完成,比较符合国人的习惯,比如简单的Db::query().Db::execute(),还有复杂的链式操作Db::where('id=1')->select ...
- php综合运用技术
五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transfer Protocol,文件传输协议,是应用层的 ...
- Scala入门系列(十):函数式编程之集合操作
1. Scala的集合体系结构 Scala中的集合体系主要包括(结构跟Java相似): Iterable(所有集合trait的根trait) Seq(Range.ArrayBuffer.List等) ...