[LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8, A solution set is: [1, 7] [1, 2, 5] [2, 6] [1, 1, 6]
class Solution {
public:
vector<int> num;
int target;
vector<vector<int> > result;
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
this->num = num;
this->target = target;
if(num.size()==){
vector<int> tmp;
result.push_back(tmp);
return result;
}
sort(this->num.begin(),this->num.end());
for(int i=;i<num.size();i++){
vector<int> tmp(,this->num[i]);
recursion(i+,tmp,this->num[i]);
}
return result;
}
private:
void recursion(int start,vector<int> tmp,int sum){
if(sum == target && find(result.begin(),result.end(),tmp)==result.end()){
result.push_back(tmp);
return;
}
vector<int> tmp0(tmp);
int sum0 = sum;
for(int i=start;i<num.size();i++){
tmp.push_back(num[i]);
sum += num[i];
if(sum<target)
recursion(i+,tmp,sum);
else if(sum == target){
if(find(result.begin(),result.end(),tmp)==result.end())
result.push_back(tmp);
}
else
break;
tmp = tmp0;
sum = sum0;
}
}//end func
};
[LeetCode] Combination Sum II (递归)的更多相关文章
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- 【LINUX/UNIX网络编程】之使用SOCKET进行UDP编程
先看任务需求: 实验二 UDP数据发送与接收 [实验目的] 1.熟练掌握套接字函数的使用方法. 2.应用套接字函数完成基本UDP通讯,实现服务器与客户端的文件传送 [实验学时] 4学时 [实验内容] ...
- 转盘游戏[XDU1006]
Problem 1006 - 转盘游戏 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 87 Accep ...
- C# 文件读写FileInfo
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Consol ...
- Underscore.js 中 _.throttle 和 _.debounce 的差异
两个方法都是用来控制事件的频率的,在mousemove,resize等这种高频率触发事件中,控制其响应频率可以明显提高程序的流畅性,减少资源的占用. 通过分析其源代码: _.throttle方法源码 ...
- TYVJ P1037 阶乘统计2 Label:坑
描述 n的阶乘定义为n!=1*2*3*……*n 如3!=6 n!通常最后会有很多0,如5!=120 最后有一个0,现在统计n!去除末尾的0后,最后k位是多少 输入格式 第一行包括两个数n,k 输 ...
- HDU 4533 威威猫系列故事——晒被子
题目链接 扫描线可做,然后当时比赛后问虎哥,他说可以标记,然后拖了很久,今天从早上折腾到晚上,终于把两种情况写出来,分析太弱.改天扫描线,再来一次. 被子如果被y = x 穿过,可以分成两部分,上和下 ...
- java定时器的使用
定时器类Timer在java.util包中.使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,设定指定的任务task在指定的延迟delay后 ...
- CentoS 下报的 Requires: perl(:MODULE_COMPAT_5.8.8)
yum error requires: libtcmalloc.so.4 rpm -Uvh http://ceph.com/rpm-cuttlefish/el6/x86_64/ceph-release ...
- 【C语言】15-预处理指令1-宏定义
预处理指令简介 1.C语言在对源程序进行编译之前,会先对一些特殊的预处理指令作解释(比如之前使用的#include文件包含指令),产生一个新的源程序(这个过程称为编译预处理),之后再进行通常的编译 2 ...
- CreateFeatureClass COM异常
private static IFeatureClass CreatStnShp(string shp) { //打开工作空间 IWorkspaceFactory wsfactory = new Sh ...