问题

该文章已迁移至个人博客【比特飞】,单击链接 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. 史上最全的 jmeter 获取 jdbc 数据使用的4种方法——(软件测试Python自动化)

    周五,下班了吗?软件测试人. 明天是周末了!给大家推荐一个技术干货好文.史上最全的 jmeter 获取 jdbc 数据使用的四种方法.我也精剪了jmeter的自动化接口测试的视频放在了同名UP主,周末 ...

  2. ajax异步上传图片&SpringMVC后台代码

    function uploadPic(){ var options = { url : "/upload/updatePic.action", type : "post& ...

  3. 修改map中原来的各种Key

    简单描述: 做数据迁移的时候,需要展示数据库的字段信息,但是我发现 Oracle的sql查询到的结果 出来默认是大写的 和 前端vue的参数小写开头+驼峰 不太一样 所以后台取到的数据都是是引用Lis ...

  4. 深度学习中损失值(loss值)为nan(以tensorflow为例)

    我做的是一个识别验证码的深度学习模型,识别的图片如下 验证码图片识别4个数字,数字间是有顺序的,设立标签时设计了四个onehot向量链接起来,成了一个长度为40的向量,然后模型的输入也是40维向量用s ...

  5. PHP入门之类型与运算符

    前言 PHP对于大部分人来说,是比较容易入门的.笔者也是刚学习不久,所以就把自己学习的基础知识进行总结和整理.第一部分是类型与运算符.如果你想学习PHP,可以参考PHP学习手册学习,任何一本教学资料也 ...

  6. linux dig 命令使用

    linux dig 命令使用方法 2018.04.20 15:47 43101浏览   dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的 ...

  7. Nginx配置各种响应头防止XSS,点击劫持,frame恶意攻击

    为什么要配置HTTP响应头? 不知道各位有没有被各类XSS攻击.点击劫持 (ClickJacking. frame 恶意引用等等方式骚扰过,百度联盟被封就有这些攻击的功劳在里面.为此一直都在搜寻相关防 ...

  8. Fortify Audit Workbench 笔记索引

    Password Management: Password in Configuration File(明文存储密码) https://www.cnblogs.com/mahongbiao/p/124 ...

  9. Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|

    内容简介: <Python自动化运维:技术与最佳实践>一书在中国运维领域将有“划时代”的重要意义:一方面,这是国内第一本从纵.深和实践角度探讨Python在运维领域应用的著作:一方面本书的 ...

  10. Django学习路27_HTML转义

    谨慎使用 自动渲染语法 {{code|safe}} urls.py 中添加对应的函数 url(r'getcode',views.getcode) 在 views.py 中添加 def getcode( ...