题目

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;
};

GitHub测试程序源码

LeetCode(39) Combination Sum的更多相关文章

  1. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  2. 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 ...

  3. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  4. LeetCode(39):组合总和

    Medium! 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. LeetCode(1)Two Sum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

随机推荐

  1. POJ-1258-Agri Ned

    链接:https://vjudge.net/problem/POJ-1258#author=fuxianda 题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么 ...

  2. linux 设置固定IP centOS6.5

    主要是要把Linux的IP固定下来,可以用另一台机器SSH连接. ping的用法: 基本语法:ping [-选项] IP地址或域名 功能描述:测试网络是否连通 常用选项:-c -c 指定发送数据包的次 ...

  3. JAVA常用知识总结(八)——计算机网络

    GET 和 POST 的区别? get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比post更不安全,因为参数直 ...

  4. UESTC - 1544 当咸鱼也要按照基本法 组合数学 容斥原理

    http://acm.uestc.edu.cn/#/problem/show/1544 考虑一下2.2.2这样的情况.答案应该是n / 2 如果只选一个的情况下,对答案的贡献是正的,但是这里有三个,也 ...

  5. odoo8 报表页面修改和字体设置

    版本8.0, 想要发票修改报表页眉的内容,去公司设置下修改,返现无论如何也不生效. 放狗后得知: You probably already know that you can customise th ...

  6. 牛客网Java刷题知识点之什么是迭代器

    不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=20 ...

  7. linux安装redis官方教程

    官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...

  8. 状态模式和php实现

    状态模式: 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类.其别名为状态对象(Objects for States),状态模式是一种对象行为型模式. 模式分析: 在很多情况下, ...

  9. hdu6376 度度熊剪纸条

    思路: 01背包.有些细节需要注意一下,比如k = 0的情况. 实现: #include <bits/stdc++.h> using namespace std; typedef pair ...

  10. Java jar包查询下载方法

    做过java开发的工程师,对java应用所需jar包一定不会陌生.特别是有需要搭建开发环境时,对各种jar包的需求量就会很大. 如何快速的找到自己想要的jar包,是蛮多java工程师所面临的一个难题. ...