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. 基于django的个人博客网站建立(二)

    基于django的个人博客网站建立(二) 前言 网站效果可点击这里访问 今天主要完成后台管理员登录的状态以及关于文章在后台的处理 具体内容 首先接上一次内容,昨天只是完成了一个登录的跳转,其他信息并没 ...

  2. Oracle 数据库密码过期问题

    (1)在CMD命令窗口中输入:           sqlplus 用户名/密码@数据库本地服务名 as sysdba;(如:sqlplus scott/1234@oracle1 as sysdba; ...

  3. 51nod 1202 不同子序列个数(计数DP)

    1202 子序列个数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40      子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a ...

  4. P1101 单词方阵

    题目描述 给一 n \times nn×n 的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 88 个方向的任一方向,同一单词摆放 ...

  5. Kali 中文家目录改英文目录

    中文版Kali装好之后,家目录会中文显示,不便操作 root@kali:~# ls -l drwxr-xr-x root root .0K 7月 : 公共 drwxr-xr-x root root . ...

  6. IOS开发学习笔记018- 一般控件的使用

    1.移动 2.动画 3.缩放 3.旋转 4.简化代码 5.总结 UIButton 的两种状态 normal highlighted  1.移动 OC语法规定:不允许直接修改某个对象中结构体属性的成员. ...

  7. IOS开发学习笔记012-核心语法

    1.点语法 2.成员变量的作用域 3. @property和@synthesize 4.id类型 5.构造方法 6.自定义构造方法 7.模板修改 8.Category - 分类 9.类扩展 一.点语法 ...

  8. Spring boot 整合jsp、thymeleaf、freemarker

    1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...

  9. Leetcode with Python -> Array

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  10. POJ2796 Feel Good -- 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 4015 Case T ...