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. hdoj1045 Fire Net(二分图最大匹配)

    题意:给出一个图,其中有 . 和 X 两种,. 为通路,X表示墙,在其中放炸弹,然后炸弹不能穿过墙,问你最多在图中可以放多少个炸弹? 这个题建图有点复杂orz. 建图,首先把每一行中的可以放一个炸弹的 ...

  2. ZZNU 2055(基姆拉尔森计算公式)

    题目链接 题意: 比如今天是2017年8月16日,星期三.下一个也是星期三的8月16日发生在2023年. 现在是日期是yyyy-mm-dd,我们希望你求出薛定谔会跳跃到那一年. 题解: emmmm.. ...

  3. Java虚拟机执行引擎

    执行引擎 关于执行引擎相关的部分, 在之前的博文里 Java内存区域中已经有所提及. 回顾一下: 也只有几个概念, JVM方法调用和执行的基础数据结构是 栈帧, 是内存区域中 虚拟机栈中的栈元素, 每 ...

  4. 【BZOJ2300】【HAOI2011】防线修建

    题目大意:给你m+3个点,有q个操作,每次要么询问当前点集构所构成的上凸壳总长度,要么在当前点集中删除一个点. 这题是吼题啊!!! 刚开始想着如何正常地做,考虑过用线段树维护一个区间内的凸包,发现并不 ...

  5. POJ 1089

    #include <iostream> #include <algorithm> #define MAXN 50005 using namespace std; struct ...

  6. 7.xamarin.android 发布签名与控制apk大小

    概述 做了xamarin android 后大家想打包一个apk,发布给其他人使用本章我们将带领大家如何打包签名一个apk. 打包 对于VS2017 或者是VS MAC来说打包一个APK非常简单. 首 ...

  7. 前端必备:FastStoneCapture 和 Licecap &&& mingw c++ 编译执行

    端必备:FastStoneCapture 和 Licecap FastStoneCapture这个软件非常小,只有2M多,并且其功能很强大,包括截图,录制视频,量尺,取色等等,对于前端工程师绝对是必备 ...

  8. python代码位置引发的错误

    觉得python对代码位置的要求简直是变态,缩进引发的错误我以前在博客里讲过了,如果不懂可以看看我以前的博客,今天又遇到了一个代码位置所引发的错误,现在给大家分享一下: 我想要打印出来一个5*5的实心 ...

  9. [转] TCPIP 网络协议层对应的RFC文档

    TCPIP网络协议层对应的RFC文档 RFC - Request For Comments 请求注解 TCP/IP层 网络协议 RFC文档 Physical Layer Data Link Layer ...

  10. linux 的yum源

    1.备份 防止以后要用 mkdir /etc/yum.repos.d/backup mv /etc/yum.repos.d/CentOS-*.repo /etc/yum.repos.d/backup ...