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 ...
随机推荐
- 科学计算库Numpy——文件读写
读文件 要读取的文件 有分隔符的文件 备注:delimiter分隔符. 有多余行的文件 备注:skiprows去掉几行. 指定列 备注:usecols指定使用哪几列. 写文件 保存后的文件 备注:fm ...
- (转)CocoaPods
本文转自http://nshipster.cn/cocoapods/ 文明是建立在道路,桥梁,运河,下水道,管线,电线和光纤这些基础设施之上的.只要设计和施工得当,它们可以帮助社会成倍的发展. 唯一的 ...
- docker 学习(3)
docker和宿主之间的数据共享以及docker间的数据共享仍然是让人头疼和操心的地方. 几个基本概念: docker: 一种容器管理技术,这里也指既有的开发工具链. container: 容器 im ...
- uva1422 二分法+优先队列贪心
题意:有n个任务,每个任务必须在在时刻[r, d]之内执行w的工作量(三个变量都是整数).处理器执行的速度可以变化,当速度为s时,一个工作量为w的任务需要 执行的时间为w/s个单位时间.另外不一定要连 ...
- [USACO]奶牛赛跑(逆序对)
Description 约翰有 N 头奶牛,他为这些奶牛准备了一个周长为 C 的环形跑牛场.所有奶牛从起点同时起跑,奶牛在比赛中总是以匀速前进的,第 i 头牛的速度为 Vi.只要有一头奶牛跑完 L 圈 ...
- 笔记-python-lib-内置函数
笔记-python-lib-内置函数 注:文档来源为Python3.6.4官方文档 1. built-in functions abs(x) 返回绝对值 all(iterable) re ...
- Python中*和**的区别
Python中,(*)会把接收到的参数形成一个元组,而(**)则会把接收到的参数存入一个字典 我们可以看到,foo方法可以接收任意长度的参数,并把它们存入一个元组中 >>> def ...
- luogu2023 [AHOI2009]维护序列
线段树加乘懒标记裸题. #include <iostream> #include <cstdio> using namespace std; typedef long long ...
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- Python-S9—Day85-ORM项目实战之forms组件以及Modelform补充、跨域请求及应用
01 forms组件补充1 02 forms组件补充2 03 ModelForm回顾 04 浏览器的历史 05 jsonop实现跨域请求 06 jsonop实现跨域请求2 07 jsonop实现跨域请 ...