C#LeetCode刷题之#39-组合总和(Combination Sum)
问题
该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3663 访问。
给定一个无重复元素的数组 candidates
和一个目标数 target
,找出 candidates
中所有可以使数字和为 target
的组合。
candidates
中的数字可以无限制重复被选取。
说明:
- 所有数字(包括
target
)都是正整数。 - 解集不能包含重复的组合。
输入: candidates = [2,3,6,7],
target = 7,
所求解集为: [ [7], [2,2,3] ]
输入: candidates = [2,3,5],
target = 8,
所求解集为: [ [2,2,2,2], [2,3,3], [3,5] ]
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
Input: candidates = [2,3,6,7],
target = 7,
A solution set is: [ [7], [2,2,3] ]
Input: candidates = [2,3,5],
target = 8,
A solution set is: [ [2,2,2,2], [2,3,3], [3,5] ]
示例
该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3663 访问。
public class Program {
public static void Main(string[] args) {
var candidates = new int[] { 2, 3, 6, 7 };
var target = 7;
var res = CombinationSum(candidates, target);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(List<IList<int>> candidates) {
foreach(var candi in candidates) {
foreach(var num in candi) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static List<IList<int>> CombinationSum(int[] candidates, int target) {
var res = new List<IList<int>>();
var candi = new List<int>();
Combination(candidates, 0, target, candi, ref res);
return res;
}
public static void Combination(int[] candidates,
int start,
int target,
List<int> candi,
ref List<IList<int>> res) {
if(target < 0) return;
if(target == 0) {
res.Add(candi);
return;
}
for(var i = start; i < candidates.Length; i++) {
candi.Add(candidates[i]);
Combination(candidates, i, target - candidates[i], candi.ToList(), ref res);
candi.RemoveAt(candi.Count - 1);
}
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3663 访问。
2 2 3
7
分析
显而易见, 以上算法的时间复杂度为:O(n2)O(n^2)O(n2) 。
C#LeetCode刷题之#39-组合总和(Combination Sum)的更多相关文章
- Leetcode题库——39.组合总和
@author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...
- LeetCode刷题笔记-递归-路径总和
题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...
- C#LeetCode刷题之#40-组合总和 II(Combination Sum II)
目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3666 访问. 给定一个数组 candidates ...
- C#LeetCode刷题之#112-路径总和(Path Sum)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4078 访问. 给定一个二叉树和一个目标和,判断该树中是否存在根节 ...
- C#LeetCode刷题之#404-左叶子之和(Sum of Left Leaves)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3 / ...
- [Swift]LeetCode39. 组合总和 | Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
随机推荐
- ffmpeg播放器实现详解 - 视频显示
ffplay是ffmpeg源码中一个自带的开源播放器实例,同时支持本地视频文件的播放以及在线流媒体播放,功能非常强大. FFplay: FFplay is a very simple and port ...
- jsp中获取路径信息的方法
今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...
- P1469 找筷子
摘要:有n根(n为奇数)长短不一的筷子,里面可以凑成(n-1)/2双筷子,只剩下一根不能凑对,问那根不能凑对的筷子有多长. 乍听起来好像不难,桶是一个好东西,可是一看数据:对于100%的数据,N< ...
- 01 安装Linux虚拟机
平常的工作学习中,Linux成为了一项比不可少的需要的掌握的技能,但是大部分人又不习惯于使用Linux进行生活,所以你需要在你的Windows电脑上安装一个虚拟机,那如何安装呢?其实不难,跟着我一步步 ...
- 在ASP.NET中,<%= %>和<%# %>有什么区别
asp.net中<%#%>出现在repeater gridview等控件中.用以绑定控件的datasource asp.net中<%%>的意思是 上运行c#或者vb代码,比如: ...
- SW算法求全局最小割(Stoer-Wagner算法)
我找到的唯一能看懂的题解:[ZZ]最小割集Stoer-Wagner算法 似乎是一个冷门算法,连oi-wiki上都没有,不过洛谷上竟然有它的模板题,并且2017百度之星的资格赛还考到了.于是来学习一下. ...
- 【管理员已阻止你运行此应用】windows defender图标打叉,无法打开mmc.exe解决办法
今天开机遇到一个奇怪的问题,发现windows defender图标上面打了个×: 打开按照系统提示需要restart服务,但是无法重启服务,会出现错误,然后尝试手动重启服务,准备打开管理控制台mmc ...
- 启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了。
启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了.
- Java 线程池记录
Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...
- Myeclipse-10.7.1版本破解
自从上次写了IDEA2020版本破解方式,这次写一下Myeclipse10.7.1版本破解 下方链接是IDEA破解教程 点击即可跳转 Myeclipse下载地址这里我上传到了百度网盘 这里提取码 ...