C#LeetCode刷题之#40-组合总和 II(Combination Sum II)
问题
该文章已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- C#LeetCode刷题之#39-组合总和(Combination Sum)
目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3663 访问. 给定一个无重复元素的数组 candi ...
- C#LeetCode刷题之#112-路径总和(Path Sum)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4078 访问. 给定一个二叉树和一个目标和,判断该树中是否存在根节 ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- LeetCode刷题笔记-递归-路径总和
题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...
- 【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 ...
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- Facebook没有测试工程师,如何进行质量控制的?
Facebook从04年的哈佛校园的学生项目在短短的7-8年的时间中快速增长为拥有10亿用户的世界上最大的社交网络,又一次见证了互联网创业成功的奇迹.同时它的产品研发流程也成为了众多互联网产品公司的追 ...
- 区间dp复习 之 tyvj 1198 矩阵连乘
题目描述 一个\(n*m\)矩阵由\(n\)行\(m\)列共\(n*m\)个数排列而成.两个矩阵\(A\)和\(B\)可以相乘当且仅当\(A\)的列数等于\(B\)的行数.一个\(N*M\)的矩阵乘以 ...
- IOS上传图片方向问题
在显示上传完毕的图片的时候遇到了一个问题, 图片莫名其妙被逆时针旋转了90度就很离谱 如下图 经过一番查询, 原来是 IOS 的相机拍照的时候会把方向角写入到图片里面 因为我用的是 element 的 ...
- xmake从入门到精通12:通过自定义脚本实现更灵活地配置
xmake是一个基于Lua的轻量级现代化c/c++的项目构建工具,主要特点是:语法简单易上手,提供更加可读的项目维护,实现跨平台行为一致的构建体验. 本文主要详细讲解下,如何通过添加自定义的脚本,在脚 ...
- [jvm] -- 类加载器及双亲委派模板篇
类加载器 JVM 中内置了三个重要的 ClassLoader BootstrapClassLoader(启动类加载器):最顶层的加载类,由C++实现,负责加载 %JAVA_HOME%/lib目录下的j ...
- Monster Audio 使用教程(二)效果参数的保存
点击左上角主菜单按钮,点击[保存]菜单,即可保存当前的所有效果参数. [另存为]菜单,则是把当前参数另存一个名称,然后通过[切换效果]菜单,实现效果的切换. 独立保存单个音轨的效果 点击音轨对应的[ ...
- spring oauth2+JWT后端自动刷新access_token
这段时间在学习搭建基于spring boot的spring oauth2 和jwt整合. 说实话挺折腾的.使用jwt做用户鉴权,难点在于token的刷新和注销. 当然注销的难度更大,网上的一些方案也没 ...
- 简单理解:数据库的一致性与四种隔离级别(+MySQL实现)
并行数据库存在着几种常见不一致问题: 1.更新丢失:两个并发的写进程同时修改某内容,一个没修改完提交之后另一个又提交,导致其覆盖了第一个提交的写进程内容. 2.脏读:一个操作读到了另外一个操作没有提交 ...
- 【Logisim实验】构建立即数-随机存储器-寄存器的传送
关于Logisim Logisim在仿真软件行列中算是比较直观的软件了,它能做的事情有很多,唯一不足的是硬件描述语言的支持,总体上来说适合比较底层的仿真,依赖于Hex值,通过线路逻辑设计能够较好的 关 ...
- Laravel 使用阿里云 oss 存储对象
一.下载安装 composer require jacobcyl/ali-oss-storage 二.注册服务提供者 在config/app.php的providers下添加: //阿里云OSS对象存 ...