Combination sum:

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.



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 2,3,6,7 and
target 7

A solution set is: 

[7] 

[2, 2, 3]

思路:深度优先遍历

代码:

void comb(vector<int> candidates, int index, int sum, int target, vector<vector<int>> &res, vector<int> &path)

    {

    if(sum>target)return;

if(sum==target){res.push_back(path);return;}

for(int i= index; i<candidates.size();i++)

{

path.push_back(candidates[i]);

comb(candidates,i,sum+candidates[i],target,res,path);

path.pop_back();

}

}

vector<vector<int> > combinationSum(vector<int> &candidates, int target) {

        // Note: The Solution object is instantiated only once.

        sort(candidates.begin(),candidates.end());

vector<vector<int>> res;

vector<int> path;

comb(candidates,0,0,target,res,path);

return res;

    }

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]

思路:依旧是深度优先遍历

void comb(vector<int> candidates, int index, int sum, int target, vector<vector<int>> &res, vector<int>
&path)

    {

if(sum>target)return;

if(sum==target){res.push_back(path);return;}

for(int i= index; i<candidates.size();i++)

{

path.push_back(candidates[i]);

comb(candidates,i+1,sum+candidates[i],target,res,path);

path.pop_back();

while(i<candidates.size()-1 && candidates[i]==candidates[i+1])i++;

}

}

vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {

        // Note: The Solution object is instantiated only once.

        sort(candidates.begin(),candidates.end());

vector<vector<int>> res;

vector<int> path;

comb(candidates,0,0,target,res,path);

return res;

    }

leetcode-combination sum and combination sum II的更多相关文章

  1. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  2. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  4. [LeetCode] #112 #113 #437 Path Sum Series

    首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...

  5. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  6. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  7. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  8. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  9. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  10. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

随机推荐

  1. js ---- 函数防抖

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  2. CSUOJ 1554 SG Value

    1554: SG Value Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 140  Solved: 35 Description The SG val ...

  3. linux中内存泄漏的检測(五)记录内存泄漏的代码

    到眼下为止,先后通过wrap malloc.new函数重载和计算指针内存大小的方法.基本上满足了对内存泄漏检測的须要. 假设发现了内存泄漏.那么就要找到内存泄漏的地方而且修正它了. 茫茫代码.如何去找 ...

  4. Nio学习3——基础模型:多路复用模型

    Reactor模式和NIO 本文可看成是对Doug Lea Scalable IO in Java一文的翻译. 当前分布式计算 Web Services盛行天下,这些网络服务的底层都离不开对socke ...

  5. Kali Linux下安装VMware Tools

    引言 Kali Linux是基于Debian的Linux发行版, 设计用于数字取证和渗透測试.安装Kali Linux非常easy,可是安装VMware Tools的过程就有点麻烦了,由于在安装中途会 ...

  6. Django加入JS,CSS,图片等外部文件的方法

    Django加入JS,CSS.图片等外部文件的方法 By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 在使用Django搭建站点时,往往须要使用 ...

  7. 3.JPA开发

    转自:https://blog.csdn.net/aspnetandjava/article/details/7034779 1. 什么是JPA 1. JPA(Java Persistence API ...

  8. Hp Open View安装使用视频

    去年完成的cisco works 2000操作(http://chenguang.blog.51cto.com/blog/350944/124729)视频深受广大博友欢迎许多人来信咨询,最近刚做完一个 ...

  9. JavaFx lineChart real-time Monitor

    JavaFx lineChart real-time Monitor   about memory public class EffectTest extends Application { Stac ...

  10. RFID的基本组织构成

    RFID技术由3大组件构成, 包括: 阅读器.天线.标签三大组件. 阅读器 为RFID系统最重要也是最复杂的一个组件.因其工作模式一般是主动向标签询问标识信息, 所以有时又被称为询问器(Interro ...