博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] Combination Sum and Combination SumII
阅读量:7053 次
发布时间:2019-06-28

本文共 3028 字,大约阅读时间需要 10 分钟。

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] 

class Solution {private:    vector
> ivvec;public: vector
> combinationSum(vector
&candidates, int target) { vector
ivec; sort(candidates.begin(), candidates.end()); combinationSum(candidates, 0, target, ivec); return ivvec; } void combinationSum(vector
&candidates, int beg, int target, vector
ivec) { if (0 == target) { ivvec.push_back(ivec); return; } for (int idx = beg; idx < candidates.size(); ++idx) { if (target - candidates[idx] < 0) break; ivec.push_back(candidates[idx]); combinationSum(candidates, idx, target - candidates[idx], ivec); ivec.pop_back(); } }};

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] 

与上题差别不大。依然是用DFS,基本的问题在于怎样去重。

能够添加一个剪枝: 当当前元素跟前一个元素是同样的时候。假设前一个元素被取了,那当前元素能够被取,也能够不取,反过来假设前一个元素没有取。那我们这个以及之后的所以同样元素都不能被取。

(採用flag的向量作为标记元素是否被选取)

class Solution {private:    vector
> ivvec; vector
flag;public: vector
> combinationSum2(vector
&num, int target) { vector
ivec; sort(num.begin(), num.end()); flag.resize(num.size()); for (int i = 0; i < flag.size(); ++i) flag[i] = false; combinationSum2(num, 0, ivec, target, 0); return ivvec; } void combinationSum2(vector
&num, int beg, vector
ivec, int target, int sum) { if (sum > target) return; if (sum == target) { ivvec.push_back(ivec); return; } for (int idx = beg; idx < num.size(); ++idx) { if (sum + num[idx] > target) continue; if (idx != 0 && num[idx] == num[idx - 1] && flag[idx - 1] == false) continue; flag[idx] = true; ivec.push_back(num[idx]); combinationSum2(num, idx + 1, ivec, target, sum + num[idx]); ivec.pop_back(); flag[idx] = false; } }};

版权声明:本文博主原创文章。博客,未经同意不得转载。

你可能感兴趣的文章
原来实现GCP用客户端登录这么简单啊
查看>>
JS每日一题: 请简述一下vuex实现原理
查看>>
从 TodoList 中学父子组件通信
查看>>
Spring MVC常用客户端参数接收方式
查看>>
原生js实现Ajax,JSONP
查看>>
用koa开发一套内容管理系统(CMS),支持javascript和typescript双语言
查看>>
Promise面试题,控制异步流程
查看>>
css-从笔试题中看知识
查看>>
LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
查看>>
前嗅ForeSpider教程:数据浏览与可视化
查看>>
7个开放式的前端面试题
查看>>
dubbo源码解析(二十三)远程调用——Proxy
查看>>
图片无法加载的情况下的优化
查看>>
数据结构与算法 | Leetcode 141:Linked List Cycle
查看>>
推荐给新手的35个好用的Vue开源库
查看>>
简述原型链是什么,有什么用处?若想访问一个对象的原型,应该使用什么方法?...
查看>>
[LeetCode] 675. Cut Off Trees for Golf Event
查看>>
SQLServer之锁简介
查看>>
从点餐小程序说起,谈谈如何从0到1设计一款toB类产品
查看>>
CSS相对定位和绝对定位
查看>>