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. 高阶函数sort

    排序中我们经常会用sort这个高阶函数,我们今天就来讲讲这个sort的比较机制,对于数字来说我们只需要比较他们的大小就可以了 但是 var arr =[15,81,9,4,3]; alert(arr. ...

  2. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  3. [React Native] Animate the Scale of a React Native Button using Animated.spring

    In this lesson we will use Animated.spring and TouchableWithoutFeedback to animate the scale of a bu ...

  4. theme-不同主题资源更改

    1.找到了影响桌面小部件的布局文件packages/apps/Mms$ vim res/layout/widget.xml修改里面的背景颜色属性,可以实现预期效果,至于里面的 <LinearLa ...

  5. Keil 编译环境之在线仿真调试问题

    一.问题现象: 这几天刚开始上手STM32,使用Keil 环境进行编程,然后使用ULINK2进行在线仿真,在按键处理函数程序中设置断点,却发现按了按键程序没有停在设置的断点,程序正常运行,如下图所示, ...

  6. 关于java中String的一点理解

      String类是java的最基本类之中的一个,非常好的掌握它的原理非常是必要的!   1.String的Final类型的.是不可继承 的.final类默认的方法都为final类型,保证了方法不能被 ...

  7. Android时间戳与字符串相互转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  8. AndroidStudio 内存泄漏分析 Memory Monitor

    ok.写一段内存泄漏的code private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { ...

  9. 转载的:Python os 和 os.path模块详解

    os.getcwd()获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curdi ...

  10. Docker---(8)Docker启动Redis后访问不了

    原文:Docker---(8)Docker启动Redis后访问不了 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/ ...