class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) { sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end()); vector<vector<int> > tmp;
vector<int> sel; dfs(candidates, , target, sel, tmp);
return tmp;
} void dfs(vector<int>& nums, int pos, int tar, vector<int>& sel, vector<vector<int> >& res) {
if (tar == ) {
res.push_back(sel);
return;
}
if (pos >= nums.size()) return;
int cur = nums[pos];
int add = ;
for (add = ; add <= tar; add+=cur) {
if (add != ) {
sel.push_back(cur);
}
dfs(nums, pos + , tar - add, sel, res);
}
for (int i = add/cur - ; i>; i--) {
sel.pop_back();
}
}
};

dfs搜索,通过unique操作去除重复的候选数字,避免产生重复的序列

LeetCode CombinationSum的更多相关文章

  1. leetcode — combination-sum

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  2. LeetCode CombinationSum II

    class Solution { public: vector<vector<int> > combinationSum2(vector<int> &num ...

  3. [LeetCode]sum合集

    LeetCode很喜欢sum,里面sum题一堆. 1.Two Sum Given an array of integers, return indices of the two numbers suc ...

  4. [LeetCode] Combination Sum 组合之和

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

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. [LeetCode]题解(python):039-Combination Sum

    题目来源 https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a targe ...

随机推荐

  1. 约瑟夫(Josephus)问题~转

    本文都是转的,一个是转博客,一个是转贴吧,前者详细,后者"强,无敌"! 博客转: 以前就知道约瑟夫问题是模拟,今天我才发现一些约瑟夫问题可以使用数学解法得出!真是强悍啊!约瑟夫问题 ...

  2. Dockerfile数据管理

    本文介绍Docker内部以及容器间的数据管理,在容器中管理数据主要有两种方式: 数据卷(Volumes) 挂载主机目录(Bind mounts) 数据卷 数据卷是一个可供一个或则多个目录使用的特殊目录 ...

  3. 【JS深入学习】——事件代理/事件委托

    事件代理/事件委托(event delegation) 需求一:当一个div内部有多个事件发生,给每个元素逐个添加事件十分麻烦... 需求二:在项目中我们常常需要动态的添加元素,不可避免的需要为那些未 ...

  4. Github只下载某一目录的文件

    比如要下载: https://github.com/xubo245/SparkLearning/tree/master/docs 将“tree/master”改成“trunk https://gith ...

  5. 【GDKOI2017】 两个胖子萌萌哒 小学奥数题

    题目大意:给你一个$n\times m$的网格,你要在这个网格上画三角形. 三角形的顶点只能在网格的整点上,且至少有一条边平行于$x$或$y$轴,且三角形面积为整数.问你能画多少个不同的三角形. 两个 ...

  6. 【hdu6058】 Kanade's sum 模拟

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题目大意:给你一个$1$到$n$的排列,请求出该序列所有区间中第$k$大之和,若该区间内少于$ ...

  7. 基于Web实现网络拓扑图

    想想好像好久没用写博客了! 由于最近想跳槽了(ps:尽管公司挽留,提出一些异与往常的挽留“制度”,But确实已经死心了) ,发现前一段时间一些做Hadoop,和Spark同事时常来请教网络拓扑图的有关 ...

  8. 线性表 (单链表、循环链表-python实现)

    一.线性表 线性表的定义: 线性表是具有相同数据类型的有限数据的序列. 线性表的特点: 出了第一个元素外,每个元素有且仅有一个直接前驱,除最后一个元素外有且只有一个后继. 线性表是一种逻辑结构,表示元 ...

  9. redis配置详细解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等: # # 1k => 1000 bytes # 1kb = ...

  10. Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门

    Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...