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 ...
随机推荐
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- JVM中的对象生命周期
在JVM运行空间中,对象的整个生命周期大致可以分为七个阶段:创建阶段(Creation).应用阶段(Using).不可视阶段(Invisible).不可到达阶段( Unreachable).可收集阶段 ...
- 【BZOJ3992】【SDOI2015】序列统计 EGF+多项式快速幂+循环卷积
如果是求$n$个数之和在模$m$意义下为$x$,那么做法是显然的. 但是这道题问的是$n$个数之积在模m意义下为$x$,那么做法就和上面的问题不同. 考虑如何把乘法转换成加法(求log): 题目中有一 ...
- Kubernetes使用GlusterFS实现数据持久化
k8s中部署有状态应用等需要持久化数据的应用,必不可少得用存储,k8s支持很多中存储方案,我司目前使用的存储有glusterfs(分为容器化和裸机方式).nfs供应用选用,本次就简单实战下gluste ...
- (转)PLSQL Developer 12.0.7连接Oracle12c数据库
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sl1992/article/details/80489413 1.下载安装PL/SQL Develo ...
- (转)ldd 查看程序依赖库
原文:https://blog.csdn.net/u010977122/article/details/52993560?spm=a2c4e.11153940.blogcont551034.8.4f7 ...
- 第6章—渲染web视图—SpringMVC+Thymeleaf 处理表单提交
SpringMVC+Thymeleaf 处理表单提交 thymleaf处理表单提交的方式和jsp有些类似,也有点不同之处,这里操作一个小Demo,并说明: 1.demo的结构图如下所示: pom.xm ...
- ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
环境:window 7+ruby2.33+rails5.0.. 该提示的意思是固件格式错误: 但是又没有提示是哪一行 非常蛋疼,我照成的原因居然是没有对齐,请看:(下面的activated_at没有和 ...
- 生成xml文件的步骤 -- XML的序列化器
1. 初始化一个xml的序列化器 XmlSerializer serializer = Xml.newSerializer(); 2. 设置序列化器的参数 serializer.setOutput(o ...
- Python -- Gui编程 -- Qt库的使用 -- 配置资源文件
1.源文件(qtRes.py) import sys from PyQt4 import QtCore, QtGui, uic class MyDialog(QtGui.QDialog): def _ ...