【leetcode】Combination Sum II
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<vector<int> > combinationSum2(vector<int> &candidates, int target)
{
sort(candidates.begin(),candidates.end());
vector<vector<int> > result;
vector<int> tmp;
backtracking(result,candidates,target,tmp,,);
return result;
}
void backtracking(vector<vector<int> > &result,vector<int> &candidates, int &target,vector<int> tmp, int sum,int index)
{
if(sum==target)
{
result.push_back(tmp);
return;
}
else
{
int sum0=sum;
for(int i=index;i<candidates.size();i++)
{
if(i>index&&candidates[i]==candidates[i-])
{
continue;
}
tmp.push_back(candidates[i]);
sum=sum0+candidates[i];
if(sum>target)
{
break;
}
backtracking(result,candidates,target,tmp,sum,i+);
tmp.pop_back();
}
}
}
【leetcode】Combination Sum II的更多相关文章
- 【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- 【Leetcode】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Combination Sum (middle)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode】Path Sum II 二叉树递归
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- usefull-url
http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...
- c#截图
private void Form_Load(object sender, EventArgs e){ //接收web url string colle = string.Empty; stri ...
- 【kAri OJ605】陈队的树
时间限制 1000 ms 内存限制 65536 KB 题目描述 陈队有N棵树,有一天他突然想修剪一下这N棵树,他有M个修剪器,对于每个修剪器给出一个高度h,表示这个修剪器可以把某一棵高度超过h的树修剪 ...
- Yii2请求,报400错误
出现400错误是yii2.0的csrf防范策略导致 在components里面添加request配置如下: 'request' => [ // !!! insert a secret key i ...
- 蝙蝠算法-python实现
BAIndividual.py import numpy as np import ObjFunction class BAIndividual: ''' individual of bat algo ...
- Treap实现山寨set
treap插入.删除.查询时间复杂度均为O(logn) treap树中每个节点有两种权值:键值和该节点优先值 如果只看优先值,这棵树又是一个堆 treap有两种平衡方法:左旋&右旋 inser ...
- jQuery返回顶部(精简版)
jQuery返回顶部(精简版) <!DOCTYPE html><html lang="en"><head> <meta charset=& ...
- 兼容amd,commonjs和browser的模块写法
从uuid.js中抽出来的写法. (function() { var _global = this; // Export public API var obj = {}; obj.attr = fun ...
- python购物&常用字符处理方法
python 一个购物车的例子 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 '''购物车''' 4 5 goods = [ 6 7 {&quo ...
- 数据库的模糊查询mybatis
<!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...