问题

该文章已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. Leetcode题库——39.组合总和

    @author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...

  2. LeetCode刷题笔记-递归-路径总和

    题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...

  3. C#LeetCode刷题之#40-组合总和 II(Combination Sum II)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3666 访问. 给定一个数组 candidates ...

  4. C#LeetCode刷题之#112-路径总和​​​​​​​(Path Sum)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4078 访问. 给定一个二叉树和一个目标和,判断该树中是否存在根节 ...

  5. C#LeetCode刷题之#404-左叶子之和​​​​​​​​​​​​​​(Sum of Left Leaves)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3      / ...

  6. [Swift]LeetCode39. 组合总和 | Combination Sum

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  7. [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  9. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集   67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...

随机推荐

  1. 手写简易SpringMVC

    手写简易SpringMVC 手写系列框架代码基于普通Maven构建,因此在手写SpringMVC的过程中,需要手动的集成Tomcat容器 必备知识: Servlet相关理解和使用,Maven,Java ...

  2. ajax+jquery+JSON笔记

    ajax (asynchronous javascript and xml -- 基于javascript和xml的异同步通讯技术)    特征: 异步通讯  异步的请求-响应模式     1.传统的 ...

  3. OSCP Learning Notes - Scanning(1)

    TCP vs UDP TCP: Connection-oriented Suited for applications that require high reliablity[HTTP, FTP,T ...

  4. echarts 实战 : 恼人的间隔问题

    使用 echarts 的时候,可能我们需要这个图表的间隔是固定的.比如 3个 4个 5个. (注意计算间隔数量的时候是不算 x轴 本身的.) 这个问题看似简单,其实有点麻烦. yAxis.splitN ...

  5. CSS3伪类 :empty

    :empty 种类:伪类选择器 版本:CSS3.0 用法:匹配每个没有子元素(包含文本)的元素. 例子: <!DOCTYPE html> <html> <head> ...

  6. Windows下安装Python 3.X 版本

    一. Python下载 Python官方下载地址 演示下载的版本为Python 3.8.3 ,你可以根据自己的选择安装其他版本的Python 二. Python 安装 下载完安装包双击安装时出错(Wi ...

  7. Mysql5.7前后修改用户密码变化

    本文主要强调修改密码的sql语句变化.如果是root密码忘记了,请参考Mysql忘记root密码怎么解决 Mysql 5.7以前修改密码 update mysql.user set password= ...

  8. Dcoker docker: Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE).

    https://stackoverflow.com/questions/46533482/error-starting-userland-proxy-bind-for-0-0-0-080-unexpe ...

  9. Tomcat Script(python)

    由于刚接触 Python,所以使用Python 书写一些小的脚本,进行备忘同时分享给大家 #!/usr/bin/env python # _*_coding:utf-8_*_ # author: 'l ...

  10. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...