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 ...
随机推荐
- wcf第三方客户端与wcf服务之间调用入门
Wcf服务与我们的客户端如何建立联系的呢.本文简单记录一下 1.创建我们的wcf服务程序. 第一个wcf服务库是创建我们的wcf库,运行时会单独来托管我们的程序,而非托管在iis下. 第二个wcf服务 ...
- 「微信小程序免费辅导教程」25,基本内容组件text的使用及个人帐号允许的服务类目
- C# datagridview列绑定类中类的属性
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://www.cnblogs.com/linghaoxinpian/p/5906374. ...
- 如何拿到半数面试公司Offer——我的Python求职之路(转)
从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...
- Jquery 实现层的拖动,支持回调函数
最近在写一个CMS内容管理系统,前台基本是用ajax异步请求服务器,通过ashx处理,返回json格式处理.由于需要更加人性化的界面,所以采用到了拖动层的操作. 以下是拖动层的主要核心方法,本来想写成 ...
- leetcode 【 Pascal's Triangle 】python 实现
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- 【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树
题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node w ...
- BZOJ4555 [Tjoi2016&Heoi2016]求和 【第二类斯特林数 + NTT】
题目 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: S(i, j)表示第二类斯特林数,递推公式为: S(i, j) = j ∗ S(i − 1, j) + ...
- 小w的喜糖(candy)
小w的喜糖(candy) 题目描述 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那会有多少种方案使得每 ...
- 多啦A梦的制作
小叮当简单颜色单一,操作起来也很容易上手.接下来的一个实例就是用css画出一个多啦A梦,首先将其分为头部,和身体.然后,再根据身体各部分细节进行进一步的具体刻画. 由于最近一直在学习JavaWeb方面 ...