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

LeetCode -- Combination Sum III

题目描述:


Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.


Ensure that numbers within the set are sorted in ascending order.




Example 1:


Input: k = 3, n = 7


Output:


[[1,2,4]]


Example 2:


Input: k = 3, n = 9


Output:


[[1,2,6], [1,3,5], [2,3,4]]




就是从1到9构成的数组{1,2,3,4,5,6,7,8,9}中,找到一种组合,包含K个元素,和为n。


思路:
是一个典型的回溯问题,也算是问题:从数组nums中找到所有和为n的组合情况的子问题。 
运用回溯模型直接解即可。 在遍历过程中,只需要收集只有k个元素的组合。






实现代码:


public class Solution {
    public IList<IList<int>> CombinationSum3(int k, int n) 
    {
       	var result = new List<IList<int>>();
    	Find(1, n, k, new List<int>(), ref result);
    	return result;
    }


private void Find(int start, int target, int k, IList<int> current, ref List<IList<int>> result)
{
	if(current.Count > k || target < 0)
	{
		return;
	}
	
	if(target == 0)
	{
		if(current.Count == k){
			result.Add(new List<int>(current));
		}
		//Console.WriteLine(current);
		return;
	}
	
	for(var i = start; i < 10; i++)
	{
		current.Add(i);
		Find(i + 1, target - i, k, current,ref result);
		current.RemoveAt(current.Count-1);
	}
}


}


相关文章:

  • 怎样使用深度纹理
  • LeetCode -- Expression Add Operators
  • [Web 开发] 获取页面元素的坐标及大小
  • LeetCode -- First Bad Version
  • 本地SPAN和远程SPAN监控原理
  • LeetCode -- House Robber II
  • 打破传统,实战至上的网管师认证
  • LeetCode -- Invert Binary Tree
  • 使用简单ORM开发框架进行快速开发
  • LeetCode -- Largest Number
  • LeetCode -- Length of last word
  • 编程是一种“组合的艺术”
  • LeetCode -- Majority Element II
  • 3G上网卡招标,华为成最大赢家
  • LeetCode -- Nim Game
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • CODING 缺陷管理功能正式开始公测
  • Java程序员幽默爆笑锦集
  • Mysql数据库的条件查询语句
  • Sequelize 中文文档 v4 - Getting started - 入门
  • vue-loader 源码解析系列之 selector
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 基于游标的分页接口实现
  • 技术:超级实用的电脑小技巧
  • 前嗅ForeSpider中数据浏览界面介绍
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 最近的计划
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • #NOIP 2014# day.2 T2 寻找道路
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (06)Hive——正则表达式
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (一)80c52学习之旅-起始篇
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .net core 依赖注入的基本用发
  • .NET Framework 4.6.2改进了WPF和安全性
  • .net framework profiles /.net framework 配置
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [20170705]diff比较执行结果的内容.txt
  • [20190416]完善shared latch测试脚本2.txt
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [30期] 我的学习方法
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-高中低端机性能适配策略
  • [ERROR]-Error: failure: repodata/filelists.xml.gz from addons: [Errno 256] No more mirrors to try.
  • [iOS]GCD(一)
  • [loj6039]「雅礼集训 2017 Day5」珠宝 dp+决策单调性+分治
  • [Mac软件]Goldie App v2.2 Mac黄金比例设计工具
  • [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'XXX' 无效
  • [pytorch入门] 3. torchvision中的transforms
  • [ThinkPHP]Arr返回1
  • [Unity3d for android]屏幕触摸事件