当前位置: 首页 > news >正文

LeetCode -- Combination Sum

题目描述:


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


The same repeated number may be chosen from C unlimited number of times.


Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7, 
A solution set is: 
[7] 
[2, 2, 3] 


给定一个数组,和一个目标数,使用给定数组中的数字,求出所有和等于目标数的可能组合。 例如给定2,3,6,7 ,可能组合是[7]和[2,2,3]
这个题目有两个条件:
1. 数字是可以重复的
2. 组合必须是升序排列
3. 不能有重复组合
4. 所有数字都是正数


思路:
对数组遍历,缩小目标数: target-= candidate[i]。如果target < candadite[i] ,中断循环
使用1个数组:arr记录当前遍历情况,如果target为0,存入结果。
在遍历数组过程中,添加当前元素:arr.Add(self),进入递归
拿掉当前元素: arr.Remove(self)




实现代码:





public IList<IList<int>> CombinationSum(int[] candidates, int target)
{
	if(candidates == null || candidates.Length == 0){
		return null;
	}


	var arr = candidates.OrderBy(x=>x).ToList();
	
	IList<IList<int>> result = new List<IList<int>>();
	Travel(arr ,new List<int>(), 0, target, result);
	
	return result;
}




private void Travel(IList<int> candidates, IList<int> arr, int index, int target, IList<IList<int>> result){
	if(target == 0 ){
		result.Add(new List<int>(arr));
		return ;
	}
	
	for(var i = index ;i < candidates.Count; i++){
		if(target < candidates[i]){
			return;
		}
		
		arr.Add(candidates[i]);
		Travel(candidates, arr, i + 1, target - candidates[i], result);
		arr.Remove(candidates[i]);
	}
}


相关文章:

  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • LeetCode -- Isomorphic Strings
  • LeetCode -- Balanced Binary Tree
  • Linux系统信息查看命令大全
  • LeetCode -- Merge Two sorted lists
  • [译]如何构建服务器端web组件,为何要构建?
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【node学习】协程
  • angular2 简述
  • Apache的基本使用
  • ES6核心特性
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • Java|序列化异常StreamCorruptedException的解决方法
  • orm2 中文文档 3.1 模型属性
  • RxJS: 简单入门
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • web标准化(下)
  • yii2中session跨域名的问题
  • 闭包--闭包作用之保存(一)
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 关于 Cirru Editor 存储格式
  • 记录:CentOS7.2配置LNMP环境记录
  • 简析gRPC client 连接管理
  • 老板让我十分钟上手nx-admin
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 使用common-codec进行md5加密
  • 使用Swoole加速Laravel(正式环境中)
  • 推荐一个React的管理后台框架
  • 移动端解决方案学习记录
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • 阿里云API、SDK和CLI应用实践方案
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 大数据全解:定义、价值及挑战
  • 正则表达式-基础知识Review
  • ###C语言程序设计-----C语言学习(3)#
  • #162 (Div. 2)
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • $jQuery 重写Alert样式方法
  • (02)vite环境变量配置
  • (8)STL算法之替换
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (十) 初识 Docker file
  • (一)为什么要选择C++