LeetCode CombinationSum
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的更多相关文章
- leetcode — combination-sum
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- LeetCode CombinationSum II
class Solution { public: vector<vector<int> > combinationSum2(vector<int> &num ...
- [LeetCode]sum合集
LeetCode很喜欢sum,里面sum题一堆. 1.Two Sum Given an array of integers, return indices of the two numbers suc ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [LeetCode]题解(python):039-Combination Sum
题目来源 https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a targe ...
随机推荐
- 合服导致 globalserver 起不来的问题
globalserver 报错 RMIInitArmyBackObject InitError 根据报错信息一路追查下来,发现某个帮派的数据解析 json 的时候报错.监视变量,找出这段字符串,大致结 ...
- Flask从入门到精通之静态文件
Web 程序不是仅由Python 代码和模板组成.大多数程序还会使用静态文件,例如HTML代码中引用的图片.JavaScript 源码文件和CSS. 在前面的章节中,我们曾检查hello.py 程序的 ...
- webpack快速入门——CSS文件打包
1.在src下新建css文件,在css文件下新建index.css文件,输入以下代码 body{ background:pink; color:yellowgreen; } 2.css建立好后,需要引 ...
- JVM内存参数( -Xms -Xmx -Xmn -Xss 直接内存)
JVM调优总结 -Xms -Xmx -Xmn -Xss jvm 内存 在不同的情况下如何增大 及 PermGen space 相关 JVM日志和参数的理解 JVM崩溃Log日志分析 -Xms 为jvm ...
- eclipse上搭建Spring环境
搭建环境之前要下载Spring Tool Suite和Spring framework 1.Spring IDE 下载(也叫Spring Tool Suite 简称 STS),进官网,直接给链接htt ...
- iOS 枚举讲解
枚举增强程序的可读性,用法上还是需要注意的 1.C语言的写法 enum XMPPReconnectFlags { kShouldReconnect = 1 << 0, // If set, ...
- POJ 1125
#include<iostream> #include<stdio.h> #define MAXN 102 #define inf 100000000 using namesp ...
- 剑指offer五十三之表示数值的字符串
一.题目 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3.1 ...
- APACHE 禁止通过IP直接访问
若是开通了虚拟主机,则需要在httpd-vhosts.conf中修改配置如下:若没有开通虚拟主机,则可以直接在httpd.conf文件最后面,加入以下代码: NameVirtualHost XXX.X ...
- 关于符号Symbol第二篇
来看一下继承自Symbol的具体实现类. 1.TypeSymbol /** A class for type symbols. * Type variables are represented by ...