博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
39. Combination Sum
阅读量:5345 次
发布时间:2019-06-15

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

1. 每个元素可用多次

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 cal(vector
& candidates, int start, int target, vector
& v, vector
>& ans){ if(0 == target) { ans.push_back(v); return; } if(target < 0) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i, target-candidates[i], v, ans); v.pop_back(); }}vector
> combinationSum(vector
& candidates, int target) { vector
> ans; vector
v; sort(candidates.begin(), candidates.end()); cal(candidates, 0, target, v, ans); return ans;}

 

2. 每个元素只能用一次

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 cal(vector
& candidates, int start, int target, vector
& v, vector
>& ans){ if(0 == target) { ans.push_back(v); return; } if(target < 0) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i+1, target-candidates[i], v, ans); v.pop_back(); }}vector
> combinationSum2(vector
& candidates, int target) { vector
> ans; vector
v; sort(candidates.begin(), candidates.end()); cal(candidates, 0, target, v, ans); return ans;}

if(i > start && candidates[i] == candidates[i-1])continue;中的i > start可以保证没有重复结果。

 

3. 数字范围1-9,给定个数。

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

 [[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]
void cal(vector
& candidates, int start, int target, int k, vector
& v, vector
>& ans){ if(0 == target && 0 == k) { ans.push_back(v); return; } if(target < 0 || 0 == k) return; int n = candidates.size(), i; for(i = start; i < n; i++) { if(i > start && candidates[i] == candidates[i-1]) continue; v.push_back(candidates[i]); cal(candidates, i+1, target-candidates[i], k-1, v, ans); v.pop_back(); }}vector
> combinationSum3(int k, int target) { vector
> ans; vector
candidates, v; for(int i = 1; i <= 9; i++) candidates.push_back(i); cal(candidates, 0, target, k, v, ans); return ans;}

 

转载于:https://www.cnblogs.com/argenbarbie/p/5245416.html

你可能感兴趣的文章
Intellij idea创建javaWeb以及Servlet简单实现
查看>>
代理网站
查看>>
Open multiple excel files in WebBrowser, only the last one gets activated
查看>>
FFmpeg进行视频帧提取&音频重采样-Process.waitFor()引发的阻塞超时
查看>>
最近邻与K近邻算法思想
查看>>
【VS开发】ATL辅助COM组件开发
查看>>
FlatBuffers In Android
查看>>
《演说之禅》I &amp; II 读书笔记
查看>>
thinkphp3.2接入支付宝支付接口(PC端)
查看>>
response和request
查看>>
【转】在Eclipse中安装和使用TFS插件
查看>>
回到顶部浮窗设计
查看>>
C#中Monitor和Lock以及区别
查看>>
【NOIP2017】奶酪
查看>>
$ 一步一步学Matlab(3)——Matlab中的数据类型
查看>>
5.6.3.7 localeCompare() 方法
查看>>
Linux下好用的简单实用命令
查看>>
常用web字体的使用指南
查看>>
描绘应用程序级的信息
查看>>
poj2406-Power Strings
查看>>