给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

所有数字(包括 target)都是正整数。
解集不能包含重复的组合。 
示例 1:

输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ]

示例 2:

输入: candidates = [2,3,5], target = 8, 所求解集为: [   [2,2,2,2],   [2,3,3],   [3,5] ]

先排序,后剪枝,避免重复。

这类问题解决重复的操作一般是先进行排序。

bool cmp1(int x, int y)
{
return x < y;
}

class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > combinationSum(vector<int>& candidates, int target)
{
int len = candidates.size(www.dasheng178.com);
sort(candidates.begin(), candidates.end(), cmp1);
vector<int> temp;
DFS(candidates, target, temp, 0);
return res;
}

void DFS(vector<int> candidates, int val, vector<int> &v, int cnt)
{
if(val == 0)
{
res.push_back(www.gcyl159.com/);
return;
}
for(int i = cnt; i <www.michenggw.com/ candidates.size(); i++)
{
if(candidates[i] <www.yigouyule2.cn = val)
{
v.push_back(candidates[i]);
DFS(candidates, val - candidates[i], v, i);
v.pop_back();
}
}
}

Leetcode39.Combination Sum组合总和的更多相关文章

  1. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  2. 039 Combination Sum 组合总和

    给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明:    所有数字(包括目标)都是正整数.    解集合 ...

  3. [LeetCode] Combination Sum 组合之和

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

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

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

  5. [leetcode]39. Combination Sum组合之和

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

  6. LeetCode OJ:Combination Sum (组合之和)

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

  7. Leetcode40. Combination Sum组合总数2 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  8. LeetCode39 Combination Sum

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

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. Suricata的规则解读(默认和自定义)

    不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...

  2. input标签属性

    很多时候,我们都用到了很多标签实现输入功能,所以在这里梳理一下. 1.建立一个文本框 <input type="text" name="userName" ...

  3. 关于HashMap中hash()函数的思考

    关于HashMap中hash()函数的思考 JDK7中hash函数的实现   static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...

  4. Java URL 中文乱码解决办法

    一. 统一所有的编码格式 (1)JSP页面设置:<%@ page language="java" import="java.util.*" pageEnc ...

  5. PHP一句话后门过狗姿势万千之传输层加工

    既然木马已就绪,那么想要利用木马,必然有一个数据传输的过程,数据提交是必须的,数据返回一般也会有的,除非执行特殊命令. 当我们用普通菜刀连接后门时,数据时如何提交的,狗狗又是如何识别的,下面结合一个实 ...

  6. Dockerfile 中的 CMD和ENTRYPOINT 两兄弟

    CMD 先说老大 CMD 当一个容器准备好运行之后,需要找一个指定命令来创建一个初始进程并运行. 一,/bin/sh -c 因为某种意义上一个Dockerfile其实可以理解是一个简化版bash 脚本 ...

  7. (转)使用CGLIB实现AOP功能与AOP概念解释

    http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...

  8. 数组、list排序

    //数字排序 int[] intArray = new int[] {4, 1, 3, -23}; Arrays.sort(intArray); 输出: [-23, 1, 3, 4] //字符串排序, ...

  9. python基础一 day5 复习

    bytes类型默认编码方式是utf-8和gbk,反正不是unicode strn内部的编码方式是Unicode         range相当于只有一次,第一次之后相当于一个有序列表.range顾头不 ...

  10. 从postgres数据库逆向生成hibernate实体类

    最近整理 一个项目,原先的项目是用的oracle,然而新的项目要用postgresql.将oracle数据库导出之后,通过powerdesigner整理,导出postgresql的脚本,然后在post ...