题目:

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]
]

  

题解:

Solution 1 ()

class Solution {
public:
void dfs(vector<vector<int>>& vv, vector<int>& v, vector<int> candidates, int target, int sum) {
if(sum == target) {
vector<int>* tmp = new vector<int>;
*tmp = v;
sort((*tmp).begin(), (*tmp).end());
if(find(vv.begin(), vv.end(), *tmp) == vv.end())
vv.push_back(*tmp);
delete tmp;
return;
}
if(sum > target) return;
for(int i=; i<candidates.size(); ++i) {
v.push_back(candidates[i]);
dfs(vv, v, candidates, target, sum+candidates[i]);
v.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> vv;
vector<int> v;
dfs(vv, v, candidates, target, );
return vv;
}
};

【LeetCode】039. Combination Sum的更多相关文章

  1. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  2. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  3. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  4. 【LeetCode】39. Combination Sum (2 solutions)

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

  5. 【LeetCode】040. Combination Sum II

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

  6. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...

  8. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  9. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

随机推荐

  1. Hibernate学习五----------组件属性

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  2. caffe编译的问题 找不到opencv的 tiff库文件

    解决办法:    sudo  su cmake  .. make  -j8 make  pycaffe make  install 问题解决. 看起来是权限问题导致.

  3. ubuntu16.04----jdk---install----config

    1.下载jdk. 2.验证java是否安装,使用java -version命令,如下图所示说明没有安装: 3.在usr目录中创建一个jdk-8目录,如下图所示: 4.配置系统环境变量,编辑/etc/p ...

  4. 通信协议之sdp---sdp会话协议

    (1)sdp 描述格式 (2)sdp example (3) sdp (1)sdp 描述格式 m=video 1234 RTP/AVP 96a=rtpmap:96 H264a=framerate:15 ...

  5. 九度OJ 1031:xxx定律 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6058 解决:3816 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数 ...

  6. windows下安装mysql 开机启动

    1 下载地址 http://dev.mysql.com/downloads/installer/ 2 下载版本 mysql community server 5.7.x 这个版本是一个傻瓜版本,设置r ...

  7. go colly proxy 代理ip 动态 ip

    package main import ( "fmt" "github.com/gocolly/colly" "github.com/gocolly/ ...

  8. oracle 11g ocr 冗余配置

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/royjj/article/details/30506343  oracle 11g ocr 冗余 ...

  9. Java for LeetCode 112 Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  10. SpringBoot学习笔记(8):事物处理

    SpringBoot学习笔记(8):事物处理 快速入门 在传统的JDBC事务代码开发过程中,业务代码只有一部分,大部分都是与JDBC有关的功能代码,比如数据库的获取与关闭以及事务的提交与回滚.大量的t ...