LeetCode(39) Combination Sum
题目
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
分析
用递归的思想实现~
AC代码
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
if (candidates.empty() || target < 0)
return vector<vector<int> >();
ret.clear();
//将给定序列排序
sort(candidates.begin(), candidates.end());
vector<int> tmp;
combination(candidates, 0, tmp, target);
return ret;
}
//递归实现
void combination(vector<int> &candidates, int idx, vector<int> &tmp, int target)
{
if (target == 0)
{
ret.push_back(tmp);
return;
}
else{
int len = candidates.size();
for (int i = idx; i < len; i++)
{
if (target >= candidates[i])
{
tmp.push_back(candidates[i]);
combination(candidates, i, tmp, target - candidates[i]);
tmp.pop_back();
}//if
}//for
}//else
}
private:
vector<vector<int> > ret;
};
LeetCode(39) Combination Sum的更多相关文章
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- LeetCode(39):组合总和
Medium! 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(304)Range Sum Query 2D - Immutable
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- LeetCode(303)Range Sum Query - Immutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(112) Path Sum
题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode(1)Two Sum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
随机推荐
- django (一) 环境的配置及Django文件简介
1, 创建虚拟环境(virtualenv 和virtualenvwrapper) 1.1, virtualenv的概述 virtualenv是用来创建Python的虚拟环境的库,虚拟环境能够独立于真实 ...
- 描述符__get__,__set__,__delete__和析构方法__del__
描述符__get__,__set__,__delete__ 1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一 ...
- 牛客练习赛42A(字符串)
传送门 结论是:一定是选取最长的那个AB连续子串. 把题面要求的a*b + a + b转化一下成(a + 1)*(b + 1) - 1,即可发现如果选取前缀后缀不连续的两段作为答案,则显然有更优解,即 ...
- ubuntu管理apt包的常用命令
安装 apt-get install nginx #安装 apt-get install nginx --reinstall #重新安装 删除 apt-get remove nginx #卸载 apt ...
- 089 Gray Code 格雷编码
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...
- 107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II
给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 ...
- CPU占用分析
用TOP命令很容易定位到时谁占用CPU最高 多线程的进程,我们要知道实际上占用cpu的最小单位是线程,所以肯定是众线程中的某一个或几个占用CPU过高导致的.top -H -p pid命令查看进程内各个 ...
- js 中对字符串的操作
1.split() split() 方法用于把一个字符串分割成字符串数组. 用法:stringObject.split(separator,howmany) separator:必选,类型为字符串或者 ...
- PM2常用命令
安装pm2 npm install -g pm2 1.启动 pm2 start app.js pm2 start app.js --name my-api #my-api为PM2进程名称 pm2 ...
- JavaScprit30-5 学习笔记
最近忙这忙那...好久没看视频学习了...但是该学的还是要学. 这次要实现的效果是利用 flex 的 特性 来实现 可伸缩的图片墙演示 页面的展示...: 效果挺炫酷啊... 那么就来总结一下 学到了 ...