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

一列数,每个数只能用一次或者不用,给出和为target的组合。

递归写的深搜,使用了编程技巧,引用。因为递归在本意上是不需要这个引用的,因为它额外的改了调用参数,所以,又有相应的 pop_back()

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target){
vector<vector<int> > ans;
if(num.size()==)
return ans; sort(num.begin(),num.end()); if(num[]>target)
return ans; //remove the elements greater than target
int len = num.size();
while(len>= && num[len-] > target)
len--;
num.resize(len); vector<int> ansPiece;
combinationSum(num,ans,target,,ansPiece); //remove duplicates
sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end()); return ans;
}
void combinationSum(vector<int> & num,vector<vector<int> > &ans,int target,int pivot,vector<int> &ansPiece)
{
if(target == )
{
ans.push_back(ansPiece);
return;
}
if( target < ||pivot == num.size())
return; combinationSum(num,ans,target,pivot+,ansPiece); ansPiece.push_back(num[pivot]);
combinationSum(num,ans,target - num[pivot],pivot+,ansPiece);
ansPiece.pop_back();
}
};
int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back(); class Solution mys;
mys.combinationSum2(num,);
}

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

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

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

  2. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

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

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

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

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

  5. LeetCode 040 Combination Sum II

    题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...

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

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

  7. LeetCode 40. Combination Sum II (组合的和之二)

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

  8. Leetcode 40. Combination Sum II

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

  9. leetcode 40 Combination Sum II --- java

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

  10. 【leetcode】Combination Sum II (middle) ☆

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

随机推荐

  1. ubuntu 压缩 解压 命令大全

    ubuntu下文件压缩/解压缩命令总结 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz ...

  2. Xadmin添加用户小组件出错render() got an unexpected keyword argument 'renderer

    环境: Python 3.5.6 Django 2.1 Xadmin 原因: render函数在django2.1上有变化 解决方案: 1.在Python终端输入命令help('xadmin') 查看 ...

  3. 洛谷 P3740 [HAOI2014]贴海报

    题目描述 Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规则如下: electo ...

  4. Linux命令之---cd

    命令简介 Linux cd 命令是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 命令格式 cd [目录名] 命令功能 切换当前目录至dirName 常用范例 ...

  5. SJTU 1077 加分二叉树

    http://acm.sjtu.edu.cn/OnlineJudge/problem/1077 题意: 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3…,n为节点 ...

  6. 如何看待微软新开源的LightGBM?

    GBDT虽然是个强力的模型,但却有着一个致命的缺陷,不能用类似mini batch的方式来训练,需要对数据进行无数次的遍历.如果想要速度,就需要把数据都预加载在内存中,但这样数据就会受限于内存的大小: ...

  7. Gpfixup

    Updated: April 17, 2012 Applies To: Windows Server 2003, Windows Vista, Windows Server 2008, Windows ...

  8. 12 JVM 垃圾回收(下)

    Java 虚拟机的堆划分 Java 虚拟机将堆划分为新生代和老年代.其中新生代又被划分为 Eden 区,以及两个大小相同的 Survivor 区. 默认情况下,Java 虚拟机采取一种动态分配的策略, ...

  9. Spring框架的AOP

    Spring学习笔记(四) 本文目录 1 AOP的介绍 2 Spring的AspectJ实现AOP(annotation) 3 Spring的AspectJ实现AOP (XML) Spring文档ht ...

  10. 团队Alpha版本冲刺(三)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最 ...