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 ...
随机推荐
- day8 python 列表,元组,集合,字典的操作及方法 和 深浅拷贝
2.2 list的方法 # 增 list.append() # 追加 list.insert() # 指定索引前增加 list.extend() # 迭代追加(可迭代对象,打散追加) # 删 list ...
- Python Hacking Tools - Port Scanner
Socket Programming 1. Scan the target Vulnerable Server. And test it by telnet. 2. Write the scanne ...
- 二进制图片blob数据转canvas
javascript是有操作二进制文件的方法的,在这里就不详述了. 而AJAX的核心XMLHttpRequest也可以获取服务端给的二进制Blob. 可以参考: XMLHttpRequest Leve ...
- 题解 洛谷 P3340 【[ZJOI2014]星系调查】
根据题意,发现题目中的图,其实就是一颗树或者是一颗基环树,每个节点上有一个点对\((x,y)\),每次询问为给定端点,找一条直线到端点间的所有点的距离之和最小. 设这条直线为\(y=kx+b\),根据 ...
- Qt-操作xml文件
1 简介 参考视频:https://www.bilibili.com/video/BV1XW411x7AB?p=12 xml简介:可扩展标记语言,标准通用标记语言的子集,简称XML.是一种用于标记电 ...
- Thinkphp5-0-X远程代码执行漏洞分析(2019-1-11)
周五下午爆洞能不能让人们好好休个周末! 分析过程 本次漏洞关键位置:/thinkphp/library/think/Request.php,lines:501由图可以看到在method函数中引入了可控 ...
- Redis持久化功能
Redis为了内部数据的安全考虑,会把本身的数据以文件的形式保存在硬盘中一份,在重启之后会自动把硬盘的数据恢复到内存(redis)里面. 一.snap shotting 快照持久化 该持久化默认开启, ...
- Zuul token FIlter 验证失败结果输出
1.背景 用 postman 测试 zuul 网关 路由成功和失败的时候,发现 路由成功的时候,返回的结构体 是 json 格式,但是路由失败的时候,返回的是空. 结构体居然不一样,这对调用方来说也要 ...
- Linux系统的基本操作
20200727 Linux目录结构 基本介绍 Linux的文件系统是采用层级式的树状结构,在此结构中的最上层是/根目录 tip1:在Linux世界里,一切皆是文件 示例 /bin: bin是Bina ...
- 3-Pandas之什么是Panel?
一.什么是Panel Series:包含一维索引的一组数据 DataFrame:包含index和columns两个轴 Panel(面板):一种三维数据容器 一个Panel对象由3个轴构成: items ...