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

LeetCode -- Combination Sum II

题目描述:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.


Each number in C may only be used once in the combination.


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 10,1,2,7,6,1,5 and target 8, 
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 


就是从一个序列candidates中找出非重复元素的组合,要求和等于target。


思路:
本题与Combination Sum很类似,区别在于,回溯时,从Index处开始;而前者每次都从0开始。
1.本题解法依旧是回溯,使用一个数组arr在遍历nums时,添加nums[i],进入递归,移除nums[i]。i∈[0,n)
2.使用index变量来track下次循环的起始位置。
3.使用哈希来去重


实现代码:


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


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


    	return new List<IList<int>>(result.Select(x=>x.Value));
    }




private void Travel(IList<int> candidates, IList<int> arr, int index, int target, Dictionary<string ,IList<int>> result){
	if(target == 0 ){
	    var key = string.Join(",", arr);
	    if(!result.ContainsKey(key)){
	        result.Add(key, arr.ToList());
	    }
		
		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]);
	}
}


相关文章:

  • 学习百度、腾讯如何把产品做到极致(转载)
  • LeetCode -- Edit Distance
  • Leetcode -- Find Minimum in Rotated Sorted Array
  • SQL2005CLR函数扩展-树的结构
  • LeetCode -- Longest Consecutive Sequence
  • Flex与.NET互操作(八):使用FluorineFx网关实现远程访问
  • LeetCode -- Missing Number
  • [Windows编程] Windows 7 对多核的支持
  • LeetCode -- Palindrome Linked List
  • SSH客户端设置环境变量
  • LeetCode -- Search for a Range
  • 老子生平
  • LeetCode -- Simplify Path
  • 老子著述
  • LeetCode -- Single Number
  • 深入了解以太坊
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • 【面试系列】之二:关于js原型
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Cumulo 的 ClojureScript 模块已经成型
  • docker-consul
  • maya建模与骨骼动画快速实现人工鱼
  • MYSQL 的 IF 函数
  • ng6--错误信息小结(持续更新)
  • springboot_database项目介绍
  • Vim 折腾记
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • Vue2.x学习三:事件处理生命周期钩子
  • 记一次删除Git记录中的大文件的过程
  • 简单易用的leetcode开发测试工具(npm)
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 前端代码风格自动化系列(二)之Commitlint
  • 浅谈Golang中select的用法
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 小程序 setData 学问多
  • mysql面试题分组并合并列
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (三)c52学习之旅-点亮LED灯
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (转)Windows2003安全设置/维护
  • (转)编辑寄语:因为爱心,所以美丽
  • (转载)PyTorch代码规范最佳实践和样式指南
  • .gitignore文件_Git:.gitignore
  • .htaccess配置重写url引擎
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .NET Core中的去虚
  • .NET 程序如何获取图片的宽高(框架自带多种方法的不同性能)
  • .NET序列化 serializable,反序列化
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • ?.的用法