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 ...
随机推荐
- win10桌面显示我的电脑
1.桌面单击右键菜单栏,选中单击个性化 2.选择主题->桌面图标设置 3.勾选需要显示或不显示的图标
- 关于IDEA 单元测试时 【empty test suite】异常的分析!!
IDEA功能很强大,配置很操蛋,自从用了之后掉了很多坑!!! 这几天要用单元测试,方法完好但是就是一直报empty test suite ,WTF,类找不到 在网上反复的找答案都没有合适 静下心想想, ...
- python os模块进程函数
Table of Contents 1. 系统进程 2. 举例 2.1. os.fork 2.2. os.exec 和 os.system 2.3. os.wait 3. 总结 系统进程 今天在看&l ...
- POI-java下载excel-HSSFWorkbook
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- C# 序列化和反序列化 详解
什么是序列化以及如何实现序列化? 如何将对象数据写入 XML 文件? 如何从 XML 文件读取对象数据? 什么是序列化以及如何实现序列化? 序列化是通过将对象转换为字节流,从而存储对象或将对象传输到内 ...
- 聊聊、Integer 封装特性
前几天在公司内部群,有人分享出了一道题,问谁能口算出来,他就膜拜那个人.题目如下: Class cache = Integer.class.getDeclaredClasses()[0]: Field ...
- 【转】ugui自制摇杆
http://www.cnblogs.com/duyushuang/p/4457691.html 珍爱生命,远离插件. 以上8个字,好好理解. 反正我是这么觉得. 我说的是unity,不是魔兽世界. ...
- 禁用jQuery chosen的选择下拉菜单
想法是启用被勾掉之后,左侧下拉框禁用.这是chosen()的 disabled之后需要更新一下.就这样,还有别的方法的话请分享,O(∩_∩)O哈哈~
- Kafka 1.0版本发布
Kafka 1.0版本发布 1.0.0 2017年11月1日发布 源码下载: kafka-1.0.0-src.tgz(asc,sha512) 二进制下载: Scala 2.11 - kafka_2.1 ...
- 【bzoj3924】[Zjoi2015]幻想乡战略游戏 动态点分治
题目描述 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来,更别说和别人打 ...