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 ...
随机推荐
- 石头剪刀布Java实现
java实现石头剪刀布过程 首先来看石头剪刀布的所有可能情况,具体如下图 第一种思路是穷举所有可能,使用if条件语句,略显呆板和麻烦. 第二种思路,因为计算机存的是数字,所以我们可以从数字角度来找规律 ...
- falcon nodata 小坑一枚
按照官方文档配置完一切正常,唯独 nodata, 明明有正常的数据,但是为什么 nodata 会认为是没收到呢 困扰许久,直到看了数据库中的数据才恍然大悟 falcon_portal库中的 hosts ...
- springboot入门之简单demo
项目构建 我们采用maven构建SpringBoot工程,首先创建一个maven工程,对应的pom文件如下: <properties> <java.version>1.8< ...
- nginx请求频率限制模块ngx_http_limit_req_module
模块: ngx_http_limit_req_module 作用: 限制客户端请求频率,防止恶意攻击 配置示例: http { limit_req_zone $binary_remote_addr z ...
- 删除 Win10 更新后的 Z 盘符(已验证)
如果你有一些不希望被访客看见的文件.照片或者视频,希望将它隐藏在一个只有自己知道的地方,那么今天的这篇教程非常适合你.我们要实现的最终效果是这样的:在Win10的"此电脑"中,有这 ...
- CSS中的BFC详解
引言: 这篇文章是我对BFC的理解及总结,带你揭开BFC的面纱.你将会知道BFC是什么,形成BFC的条件,BFC的相关特性,以及他的实际应用. 一.何为BFC BFC(Block Formatting ...
- Machine learning 第8周编程作业 K-means and PCA
1.findClosestCentroids function idx = findClosestCentroids(X, centroids) %FINDCLOSESTCENTROIDS compu ...
- Win下Eclipse提交Hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denied: user=D
描述:在Windows下使用Eclipse进行Hadoop的程序编写,然后Run on hadoop 后,出现如下错误: 11/10/28 16:05:53 INFO mapred.JobClient ...
- java.io.IOException: Could not find status of job:job_1534233312603_0002
hive执行插入数据操作 报错: 在hive console里面输入: set hive.jobname.length=20; 再次执行好了:
- 用sinopia搭建内部npm服务
sinopia搭建 这里默认你已经有node环境了,执行下面命令,全局安装 sinopia npm install -g sinopia 安装好后,执行下面命令启动 sinopia sinopia 你 ...