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% 简单 ...
随机推荐
- Cyber Security - Palo Alto Firewall Objects Addresses, Services, and Groups(3)
LDAP Authentication and Remote Users and Groups Create Remote User Objects and LDAP Integration: sam ...
- Maven如何利用父工程对版本进行统一管理
项目开发中我们该怎么对项目依赖的版本进行统一管理呢 答:创建一个父级工程,让所有的业务模块都继承该父级工程,即所有的业务都为Module 在父级工程pom文件添加<dependencyManag ...
- 数字孪生,数据驱动下的北京 CBD 智能楼宇三维可视化系统
前言 楼宇作为建筑基础设施的主体,为人们提供着重要的生存空间.随着物联网.人工智能概念的兴起以及智慧城市如火如荼的开展,智能楼宇的重要性越发突显. 随着城市现代化建设的发展,建筑的智能化,特别是公用建 ...
- git安装并与远程仓库关联相关配置
git是当前最流行的版本控制系统,下面简单记录一下git的安装及其与远程仓库的关联. git安装 打开git官网,下载对应的安装包. 双击运行安装包,安装过程中可以直接选择默认配置,一路next下去. ...
- css 过渡样式 transition
过渡顾名思义就是就是样式改变的一个过程变化 简介 transition: property duration timing-function delay; 值 描述 transition-proper ...
- Java 中的链式编程
前言 在写项目的时候,有一个实体类有好多个属性,new 出来之后需要不停的使用setXXX( )方法,效率低而且代码可读性差,查询了下发现可以实现实体类的链式编程. public class Us ...
- JVM系列之:Contend注解和false-sharing
目录 简介 false-sharing的由来 怎么解决? 使用JOL分析 Contended在JDK9中的问题 padded和unpadded性能对比 Contended在JDK中的使用 总结 简介 ...
- Java Web(2)-jQuery上
一.jQuery初体验 使用jQuery给一个按钮绑定单击事件 <!DOCTYPE html> <html lang="en"> <head> ...
- 水题----B - Badge CodeForces - 1020B
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. A ...
- thymeleaf js绑定多个变量参数
写法一: <img th:src="@{/css/bianji.png}" th:onclick="|viewById('${user.id}','${user.i ...