问题

该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3666 访问。

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

输入: candidates = [10,1,2,7,6,1,5],

target = 8,

所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]

输入: candidates = [2,5,2,1,2],

target = 5,

所求解集为: [ [1,2,2], [5] ]


Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Input: candidates = [10,1,2,7,6,1,5],

target = 8,

A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]

Input: candidates = [2,5,2,1,2],

target = 5,

A solution set is: [ [1,2,2], [5] ]

示例

该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3666 访问。

public class Program {

    public static void Main(string[] args) {
var candidates = new int[] { 10, 1, 2, 7, 6, 1, 5 };
var target = 8; var res = CombinationSum2(candidates, target);
ShowArray(res); Console.ReadKey();
} private static void ShowArray(IList<IList<int>> candidates) {
foreach(var candi in candidates) {
foreach(var num in candi) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
Console.WriteLine();
} public static IList<IList<int>> CombinationSum2(int[] candidates, int target) {
var res = new List<IList<int>>();
if(candidates.Length == 1) {
if(candidates[0] == target) {
res.Add(candidates);
return res;
}
}
var candi = new List<int>();
Array.Sort(candidates);
Combination(candidates, -1, 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) {
if(!res.Any(r => r.SequenceEqual(candi))) {
res.Add(candi);
}
return;
}
for(var i = start + 1; i < candidates.Length; i++) {
candi.Add(candidates[i]);
Combination(candidates, i, target - candidates[i], candi.ToList(), ref res);
candi.RemoveAt(candi.Count - 1);
while(i < candidates.Length - 1 && candidates[i] == candidates[i + 1]) i++;
}
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3666 访问。

1 2 5
1 7
1 1 6
2 6

分析

显而易见, 以上算法的时间复杂度为:O(n2)O(n^2)O(n2) 。

C#LeetCode刷题之#40-组合总和 II(Combination Sum II)的更多相关文章

  1. C#LeetCode刷题之#39-组合总和(Combination Sum)

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

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

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

  3. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  4. Leetcode题库——40.组合总和II

    @author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...

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

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

  6. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. LeetCode 39. 组合总和(Combination Sum)

    题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...

  8. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. Docker基础使用

    Docker篇 CentOS 安装Docker - 菜鸟教程 安装须知: 1.Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更 ...

  2. Spring Boot 2.x基础教程:使用EhCache缓存集群

    上一篇我们介绍了在Spring Boot中整合EhCache的方法.既然用了ehcache,我们自然要说说它的一些高级功能,不然我们用默认的ConcurrentHashMap就好了.本篇不具体介绍Eh ...

  3. 资深CIO介绍如何选型OA系统的?

    OA办公系统成为企业管理的标配软件,在于可有效加强组织管理能力,提高员工协同效率,助力企业科学决策,合理分配企业资源,提升企业综合实力与市场竞争力.企业OA选型的经验总结来说也就是品牌.技术.产品.服 ...

  4. .clearfix 清除浮动,@import

    我们知道,在网页的DIV+CSS布局中,很多时候要用到浮动. 既然有浮动,那就有清除浮动. 清除浮动有很多种方式,而在实际项目中,比较常用的是这一种. .clearfix:after { conten ...

  5. 【JUnit测试】总结

    什么是Junit? Junit是xUnit的一个子集,在c++,paython,java语言中测试框架的名字都不相同 xUnit是一套基于测试驱动开发的测试框架 其中的断言机制:将程序预期的结果与程序 ...

  6. Crossword Answers -------行与列按序输出

    题目链接:https://vjudge.net/problem/UVA-232#author=0 题意:关键句:The de nitions correspond to the rectangular ...

  7. [leetcode/lintcode 题解] Amazon面试题:连接棒材的最低费用

    为了装修新房,你需要加工一些长度为正整数的棒材 sticks. 如果要将长度分别为 X 和 Y 的两根棒材连接在一起,你需要支付 X + Y 的费用. 由于施工需要,你必须将所有棒材连接成一根. 返回 ...

  8. xctf-pwn level3

    这道题研究了很久,总算是理解了got表和plt表的关系和作用 checksec看防护 main函数里提示了vunlnerable函数 查看一下vulnerable函数 可以利用read函数栈溢出,但是 ...

  9. JVM系列之:String.intern的性能

    目录 简介 String.intern和G1字符串去重的区别 String.intern的性能 举个例子 简介 String对象有个特殊的StringTable字符串常量池,为了减少Heap中生成的字 ...

  10. PHP log10() 函数

    实例 返回不同数的以 10 为底的对数: <?phpecho(log10(2.7183) . "<br>");echo(log10(2) . "< ...