LeetCode OJ-- Combination Sum II **
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 **的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode 040 Combination Sum II
题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- 基于django的个人博客网站建立(一)
基于django的个人博客网站建立(一) 前言 网站效果可点击这里访问 之前基于hexo和github page搭建过一个博客网页,后来由于换了个系统,感觉弄的有点麻烦也就没有再去管它了,最近偶然从网 ...
- Ubuntu强制卸载VMware-player
有时候安装了vmwar-player,想再安装vmware-workstation,却提示一些古怪的消息(现在忘记具体是什么了).只能先卸载再安装 首先你可以尝试常规卸载: sudo vmware-i ...
- momo不是玩具,.Net雄起
互联网时代 .NET 会渐渐衰落吗?一个架构师对 .NET 的思考 2015-12-14 11:03 darklx 博客园 字号:T | T 为了更好的适应互联网时代的需求,我们公司已经把我们的 .N ...
- (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中
本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...
- 3 ways of including JavaScript in HTML
Code written in JavaScript must be executed from a document written in HTML. There are three ways of ...
- 码农与UI沟通的日常
事情是这样的,这是一个兴趣群组的效果图. 我看了一眼没有帖子时的提示,觉得这样的提示 不走心 不能展现出我们团队对于人生及世界的深度理解和高尚的品格. 于是,我选择了表达内心的真实感受. 我觉得这完美 ...
- 【Luogu P1637】 三元上升子序列
对于每个数$a_i$,易得它对答案的贡献为 它左边比它小的数的个数$\times$它右边比它大的数的个数. 可以离散化后再处理也可以使用动态开点的线段树. 我使用了动态开点的线段树,只有需要用到这个节 ...
- RSA进阶之两个N的公约数
适用场景: 给你两个n,n1和n2. 两个数都很大,不好分解. 但是这数刚好有质数公因子(试试欧几里得辗转相除跑完之后,就是不断地相除就可以了,4000多位也是很快的),那不就相当于间接的分解出q或者 ...
- Python学习-day14-HTML
以下博客为转载 http://www.cnblogs.com/evilliu/p/5750539.html 一:HTML(HyperText Markup Language)介绍 超文本标记语言,标准 ...
- WordPress 通过文章 URL 获取文章 ID
// 获取当前文章链接,注意,在文章类型页面的主循环外使用标签时,如果没有指定 Post ID 参数,该标签将返回循环中最后一篇文章的 URL,而不是当前页面的固定链接,或者直接不返回结果,所以一般需 ...