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的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  3. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  5. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  6. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  7. [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...

  8. Leetcode 39. Combination Sum

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  9. leetcode 39 Combination Sum --- java

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

随机推荐

  1. springboot(十六):使用Jenkins部署Spring Boot

    jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署分为三个步骤: 第一步,jenkins安装 第二步,插件安装和配置 第 ...

  2. 操作系统学习笔记----进程/线程模型----Coursera课程笔记

    操作系统学习笔记----进程/线程模型----Coursera课程笔记 进程/线程模型 0. 概述 0.1 进程模型 多道程序设计 进程的概念.进程控制块 进程状态及转换.进程队列 进程控制----进 ...

  3. npm模块管理器入门

    什么是 NPM npm 是 Node 官方提供的包管理工具,他已经成了 Node 包的标准发布平台,用于 Node 包的发布.传播.依赖控制.npm 提供了命令行工具,使你可以方便地下载.安装.升级. ...

  4. hadoop2.5的伪分布式安装配置

    一.windows环境下安装 根据博主写的一次性安装成功了: http://blog.csdn.net/antgan/article/details/52067441 二.linux环境下(cento ...

  5. vue2.0与实战开发

    慕课网实战 百度云 web前端实战: Node.js入门到企业Web开发中的应用 Web前端性能优化 让你的页面飞起来 前端跳槽面试必备技巧 前端JavaScript面试技巧全套 node.JS 线上 ...

  6. 【转】如何使用Git上传本地项目到github?(mac版)

    原文链接:http://www.cnblogs.com/lijiayi/p/pushtogithub.html 在此假设你已经在 github 上创建好了一个项目,像这样: 并且你已经完成了自己的项目 ...

  7. 谈谈微服务中的 API 网关(API Gateway)

    前言 又是很久没写博客了,最近一段时间换了新工作,比较忙,所以没有抽出来太多的时间写给关注我的粉丝写一些干货了,就有人问我怎么最近没有更新博客了,在这里给大家抱歉. 那么,在本篇文章中,我们就一起来探 ...

  8. Java 三大特性

    一.Java第一大特性:封装   封装:将属性私有化,提供共有方法访问私有属性,实现细节隐藏,并且程序也更加容易维护. class Dish { private final String name; ...

  9. 如何配置 Health Check?- 每天5分钟玩转 Docker 容器技术(107)

    容器状态是 UP 的,应用就是健康的吗? 还真不一定!Docker 只能从容器启动进程的返回代码判断其状态,而对于容器内部应用的运行情况基本没有了解. 执行 docker run 命令时,通常会根据 ...

  10. JAVA通过Gearman实现MySQL到Redis的数据同步(异步复制)

    MySQL到Redis数据复制方案 无论MySQL还是Redis,自身都带有数据同步的机制,像比较常用的 MySQL的Master/Slave模式 ,就是由Slave端分析Master的binlog来 ...