目录

  • 问题
  • 示例
  • 分析

问题

该文章已迁移至个人博客【比特飞】,单击链接 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刷题实战81:搜索旋转排序数组 II

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  4. Leetcode刷题实战(1):Two Sum

    Leetcode不需要过多介绍了,今天一边开始刷题一边开始总结: 官网链接如下:https://leetcode.com/problemset/all/ 题1描述: 1 Two Sum 38.80% ...

  5. LeetCode刷题(40)--Search a 2D Matrix

    已经排好序的矩阵,搜索一个元素,二分法 class Solution(object):def searchMatrix(self, matrix, target):""" ...

  6. LeetCode刷题day43|1049. 最后一块石头的重量 II、 494. 目标和、474.一和零

    文章目录 一.1049. 最后一块石头的重量 II 二.494. 目标和 三.474.一和零 一.1049. 最后一块石头的重量 II 这道题其实就与"分割子集"一样,就是先将总和 ...

  7. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  8. C#LeetCode刷题之#1-两数之和(Two Sum)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3762 访问. 给定一个整数数组和一个目标值,找出数组中和为目标值 ...

  9. C#LeetCode刷题-数组

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

最新文章

  1. Android渗透测试Android渗透测试入门教程大学霸
  2. IE6/IE7下:inline-block解决方案
  3. 压缩维度oj P1173+P1174+P1164
  4. 怎样为Linux内核打补丁
  5. codeforces 850 A
  6. linux安装nagios客户端
  7. C语言重难点总结:printf和scanf
  8. MySQL日志的类型和实现方法_mysql的日志类型及作用
  9. phabricator安装配置和使用(docker安装和独立部署)
  10. 【推荐】智慧数字城管执法综合应用平台可视化系统建设架构解决方案合集(共202份,920M)
  11. 医疗设备管理系统源码【免费分享源码】
  12. 微信卡包开发(JS-JDK)
  13. 如何在家免费使用知网?
  14. PPT如何制作形状动画?
  15. c++内存分区、创建变量开辟内存
  16. plex插件显示无服务器,deepin 15.11 安装plex和插件
  17. 从Idea到付诸实践,你必须要知道的
  18. 开香港汇丰银行账户需要什么条件?办理需要多少价格?
  19. 基于libhid/libusb进行usb传输数据
  20. 【机考】华为OD2022.11.01机考题目思路与代码

热门文章

  1. mysql 用户授权
  2. django-验证码
  3. python-random种子
  4. 阅读 嘀嗒嘀嗒 微信公共号
  5. 新手零基础入门小程序之万达电影
  6. OSPF的高级应用之地址汇总与虚链路的配置
  7. Linux的cifs(samba)文件服务
  8. EF连接ORACLE
  9. cocos2dX之一——安卓环境搭建
  10. UIView 中常见的方法总结