前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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, a1a2 ≤ … ≤ 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]

2. 题意

给定一个候选集合,集合中的元素以非递减方式存放。给定一个目标值T

输出所有唯一的组合。

3. 思路

由于不能出现重复元组,所以我们先将candidate set进行排序,然后规定比新插入的元素不能比当前元素小。

采用DFS思想即可。

4: 解法

class Solution {
private:
vector<int> ans;
vector<vector<int> > ret;
public:
void DFS(int start,vector<int> &candidates, int target){
if(target==0){
ret.push_back(ans);
return;
} for(int i=start;i<candidates.size();++i){
if(target <candidates[i]) return;
ans.push_back(candidates[i]);
DFS(i,candidates,target-candidates[i]);
ans.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
ans.clear();
ret.clear();
if(!target || !candidates.size()) return ret;
sort(candidates.begin(),candidates.end());
DFS(0,candidates,target);
return ret;
}
};

 

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3896010.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解] Combination Sum的更多相关文章

  1. 回溯法 leetcode题解 Combination Sum 递归法

    题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  5. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

随机推荐

  1. C#中如何判断线程当前所处的状态

    转自原文 在C#中如何判断线程当前所处的状态 在C#中,线程对象Thread使用ThreadState属性指示线程状态,它是带Flags特性的枚举类型对象.    ThreadState 为线程定义了 ...

  2. 什么是事件代理?DOM2.0标准事件模型的三个阶段

    体验更优排版请移步原文:http://blog.kwin.wang/programming/js-event-delegation.html 事件代理,又称事件委托(Delegation),就是将处理 ...

  3. 禁用Java JDK的自动更新

  4. linux下使用adb查看android手机的logcat

    root@ubuntu:/home/song# adb logcat -s VLC

  5. 搭建Git Server - 个人开发简单搭建

    ###################### 教程一 ####################### 1. 创建git用户和用户组 #新建一个git用户组 sudo groupadd git #新建一 ...

  6. java css

    SS动画 http://daneden.github.io/animate.css

  7. Scala操作MongoDB

    Scala操作MongoDB // Maven <dependencies> <dependency> <groupId>org.mongodb</group ...

  8. Stars URAL - 1028

    就是给你一些星星的坐标,然后求出每个星星的左下角有多少颗星星 题目保证按照Y坐标的顺序给出每个星星的坐标,那么我们就可以说,当输入某个星星的坐标时,此时有多少个星星的横坐标小于它,它左下角就有多少星星 ...

  9. Unity3d 下websocket的使用

    今天介绍一下如何在Unity3D下使用WebSocket. 首先介绍一下什么是websocket,以及与socket,和http的区别与联系,然后介绍一下websocket的一些开源的项目. WebS ...

  10. Codeforces 667C DP

    题意:给你一个字符串,这个字符串的构造方法如下:先选择一个长度大于4的前缀,然后每次向字符串尾部添加一个长度为2或者长度为3的后缀,不能添加连续的相同的后缀,问可能的后缀有哪些?并按字典序输出去. 思 ...