https://oj.leetcode.com/problems/combination-sum/

给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得7的所有组合。

递归,类似深搜的思想。

class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > ans;
if(candidates.size()==)
return ans; //remove duplicates
sort(candidates.begin(),candidates.end());
vector<int>::iterator itr = unique(candidates.begin(),candidates.end());
candidates.resize(itr - candidates.begin());
if(candidates[]>target)
return ans; //remove the elements great than target
int len = candidates.size();
while(len>= && candidates[len-] > target)
len--;
candidates.resize(len); vector<int> ansPiece;
combinationSum(candidates,ans,target,,ansPiece); return ans;
}
void combinationSum(vector<int> & candidates,vector<vector<int> > &ans,int target,int pivot,vector<int> ansPiece)
{
if(target == )
{
ans.push_back(ansPiece);
return;
}
if( target < ||pivot == candidates.size())
return; int i = ;
while(target - i>=)
{
if(i != )
ansPiece.push_back(candidates[pivot]);
combinationSum(candidates,ans,target - i,pivot+,ansPiece); i = i + candidates[pivot];
}
}
};

LeetCode OJ--Combination Sum **的更多相关文章

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

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

  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 组合之和之四

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

  10. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

随机推荐

  1. Java集合框架汇总

    HashMap是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的.HashMap最多只允许一条记录的键为NUL ...

  2. pandas库Series类型与基本操作

    pandas读取excel的类型是dataFrame,然后提取每一列是一个Series类型 Series类型包括index和values两部分 a = pd.Series({'a':1,'b':5}) ...

  3. A1009 Product of Polynomials (25)(25 分)

    A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...

  4. Java文件 ---文件相关操作

    创建文件 file.createNewFile() 注:若该文件对象未指定文件路径,则文件创建于相对路径中,即工程目录下.(“../”表示上级文件目录,相对路径前面不加“/”,eg:bin/text. ...

  5. Error:Execution failed for task ':myapplication:processDebugResources'. > com.android.ide.common.pro

    Error:Execution failed for task ':myapplication:processDebugResources'. > com.android.ide.common. ...

  6. Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)

    什么是NIC Teaming?         NIC Teaming 就是将两个或更多的网络适配器组合在一起,从而达到容错和带宽聚合作用.NIC Teaming 中的每个网络适配器都是物理存在的(虚 ...

  7. 【网易严选】iOS持续集成打包(Jenkins+fastlane+nginx)

    本文来自网易云社区 作者:孙娇 严选iOS客户端的现有打包方式是通过远程连接打包机执行脚本去打包,打完包会输出相应的ipa的二维码,扫一扫二维码可以安装,但是随着测试队伍的壮大,外包同学越来越多,在打 ...

  8. AutoMapper教程

    http://www.cnblogs.com/gc2013/p/4487567.html http://www.qeefee.com/article/automapper

  9. 安卓手机关闭底部键盘灯的方法(htc G11亲测有效)

    还在因为看电子书和看电影时键盘灯刺眼而苦恼吗?下面提供一个方法关闭键盘灯,让你轻松DIY! 1、手机必须先Root。使用RE管理器,按照这个路径,找到文件:brightness sys/devices ...

  10. IOS开发学习笔记038-autolayout 自动布局 界面实现

    在storyboard/xib文件中实现自动布局 autolayout 1.注意事项 autolayout和frame属性是有冲突的,所以如果准备使用autolayout,就不要再代码中对控件的fra ...