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. Bind读取配置到C#实例

    1.创建一个空的ASP.NET Core Web 应用程序 2.程序包管理控制台执行Install-Package Microsoft.AspNetCore -Version 2.0.1 3.创建js ...

  2. centos7修改默认运行级别的变化

    在学习centos7,视频教学中使用的的centos6,查看权限文件命令为 cat /etc/inittab/ 但是在centos7中输入以下命令会出现这种情况 这个已经不用了,现在修改权限的命令修改 ...

  3. day 48 ORM 进阶 多表连接 创建 以及 html模板继承

    多表的一对多创建 多表的多对多创建 多表的一对多修改 多表的多对多修改 前情提要:   最近认识了不少大佬,大佬开着保时捷. .一顿狂奔..我连车尾灯都看不到.. 本次内容  ORM 的多表链接  查 ...

  4. POJ 1032

    #include<iostream> using namespace std; int main() { int n; int num; ; int i,j; cin>>num ...

  5. Numpy 创建数组2

    Numpy数组除了可以使用底层 ndarray 构造器来创建外,也可以同伙一下集中方式来创建. numpty.empty numpy.empty方法用来创建一个指定形状(shaoe).数据类型(dty ...

  6. Luogu P4670 [BalticOI 2011 Day2]Plagiarism 题解

    我最近是不是数据结构学傻了啊... 这道题看是1e5,所以复杂度为\(O(nlogn)\)的是完全可以跑过去的,然后看题,要求的对于每个数满足要求的区间的长度之和,我们自然而然的就可以想到用FHQ-T ...

  7. spring中的context:include-filter和context:exclude-filter的区别

    在Spring 的配置文件中有: <context:component-scan base-package="njupt.dao,njupt.service"> < ...

  8. linux安装扩展总结

    ---恢复内容开始--- 1.安装php 模块安装命令. wget http://pear.php.net/go-pear 执行 php go_pear 如果是php7 wget http://pea ...

  9. svn新增文件时自动给文件设置强制只读属性needs-lock

    1.从SVN客户端的“设置”->常规设置-> Subversion->Subversion 配置文件-> 编辑按钮 -> 打开配置文件 2.找到[miscellany], ...

  10. 【jQuery源码】preFilter

    preFilter: { "ATTR": function( match ) { //属性名解码 match[1] = match[1].replace( runescape, f ...