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. ...
随机推荐
- python采用sqlachmy购物商城
一.流程图: 二.目录结构: C:\USERS\DAISY\PYCHARMPROJECTS\S12\MARKET │ __init__.py │ __init__.pyc │ ├─backend │ ...
- html原样输出html代码
<xmp>********</xmp> 在网页上显示html代码标记<xmp></xmp>有时我们会将html代码显示在网页上,直接写会有问题, 如果我 ...
- CI模板中如何引入模板
<?php $this->load->view('index/head.html') ?>
- dispaly:none 和visibility :hidden的区别
display:none 通常被 JavaScript 用来在不删除元素的情况下隐藏或显示元素. 它和 visibility 属性不一样.把 display 设置成 none 元素不会占据它本来应该显 ...
- Symbol.iterator 和 for of
Symbol.iterator 和 for of 是es6的新特性 可以为对象设置 自己的迭代器 首先介绍我们的for of var arr = [1,2,3,8,33] for (var i of ...
- node.js0-5初级者
伴着<妈是心中的茉莉花> 这里,我用的sublime记事本,所以用的运行方法是终端.(后来发现git 可以省去cd切换目录). 安装node.js 官网说的很清楚. 这里我们可以在js文 ...
- Vue-router 的练习
使用了vue-cli 生成了一套webpack的模版. 之后在其中练习 vue-router. 以下是一些记录. 1.动态路由的配置 import Vue from 'vue' import Rout ...
- GetDC(),ReleaseDC()
用GetDC()得到的DC, 必须调用ReleaseDC()用CreateDC()创建的DC, 必须调用DeleteDC() 两者是不能混淆的.一种典型的错误代码如下:CDC* pDC = GetDC ...
- DRP项目
DRP(distribution resource planning)分销资源计划是管理企业的分销网络的系统,目的是使企业具有对订单和供货具有快速反应和持续补充库存的能力.解决了随着企业销售规模的逐渐 ...
- # iOS Block的本质(三)
iOS Block的本质(三) 上一篇文章iOS Block的本质(二)中已经介绍过block变量的捕获,本文继续探寻block的本质. 1. block对对象变量的捕获,ARC 环境 block一般 ...