39. Combination Sum

文章目录

  • 1题目理解
  • 2 回溯分析
  • 3 40. Combination Sum II
    • 3.1 延续39解题思路
    • 3.2 新思路
  • 3.3 递归计数的方式
  • 4 216. Combination Sum III

1题目理解

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

输入:int数组 int[] candidates 和 int数值 target
规则:数组中的元素可以不限次数的选择和使用,让选择的数字和等于target
输出:输出所有的组合,不能重复。

2 回溯分析

例如当前在处理candidates[index]元素,可以选择使用这个元素,时候之后还可以继续使用;也可以选择不使用这个元素,不使用的话,就继续考察下一个元素。
优化的地方是:先将数组排序,排序之后当candidates[index]>target就可以不用继续递归下去。
差点忘记了,做回溯题目最重要的是画回溯树。
在这里插入图片描述

例如 candidates=[2,3,6,7],target=7
回溯树每个节点的状态dfs(index,target),表示当前处理第index个元素,目标值是target。
当处理第0个元素的时候,可以选择元素2,也可以不选择。
选择元素2,进入状态dfs(0,5),组合=[2]。
不选择元素2,进入状态dfs(1,7),组合=[]。
这里有个奇怪的想法,我不想选择第1个2,我想选择第2个2可以吗?也就是说不选择元素2,进入状态dfs(0,7),我还想选择第二次的那个2。这里是不可以。因为这样的话,是会重复的。

class Solution {private int[] candidates;private List<List<Integer>> answer;public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);this.candidates = candidates;answer = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();dfs(0,target,list);return answer;}private void dfs(int index,int target,List<Integer> list){if(target==0){answer.add(new ArrayList<Integer>(list));}else if(index>= candidates.length){return;}else{if(target<candidates[index]){return;}list.add(candidates[index]);dfs(index,target-candidates[index],list);list.remove(list.size()-1);dfs(index+1,target,list);}}
}

时间复杂度,一个比较松的上届是O(n∗2n)O(n*2^n)O(n2n)。n是数组长度。每个位置有选和不选两种选择所以有2n2^n2n

3 40. Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

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

Note: The solution set must not contain duplicate combinations.
Constraints:

1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30

3.1 延续39解题思路

39 题数组中每个元素不重复,但是能无限池使用;40题虽然每个元素只能使用一次,但是元素可能重复。也就是说两者的区别是39对于一个元素使用次数不限,40,对于一个元素使用次数有限。那就看看每个元素能使用多少次吧。
因为 1 <= candidates[i] <= 50 ,所以可以设计一个length=51的数组,计算每个元素还能使用的次数。用count数组计算每个元素能使用的次数。
假设candidates = [10,1,2,7,6,1,5], target = 8。那么1可以使用2次,2可以使用一次,用(1,2),(2,1),(5,1),(6,1)(7,1)(10,1)这样的序列对表示。
先画个递归树。
在这里插入图片描述

这里状态dfs(num,target),num表示当前正在处理的元素。注意:这里与39不同,39是index,表示下标。这里是num,表示元素本身。
初始状态dfs(1,8)。1是candidates中的最小元素。
选择1,计数(1,1),进入状态dfs(1,7)。
不选择1,进入状态dfs(2,8)。

对于处理元素num,
如果选择num,选择之后要看num的计数是否大于0,是就进入dfs(num,target-num),不是的话,需要查看下一个计数大于0的数。进入dfs(nextNum,target-num)。

如果不选择num,那就需要查看下一个计数大于0的数。进入dfs(nextNum)。

class Solution {private int[] count = new int[51];private List<List<Integer>> answer;private int[] candidates;public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);for(int num : candidates){count[num]++;}this.candidates = candidates;answer = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();dfs(candidates[0],target,list);return answer;}private void dfs(int num,int target,List<Integer> list){if(target==0){answer.add(new ArrayList<Integer>(list));}else if(num>= count.length){return;}else{if(target<num){return;}list.add(num);count[num]--;if(count[num]>0){dfs(num,target-num,list);}else{int nextNum = num+1;while(nextNum<count.length && count[nextNum]==0){nextNum++;}dfs(nextNum,target-num,list);}list.remove(list.size()-1);count[num]++;int nextNum = num+1;while(nextNum<count.length && count[nextNum]==0){nextNum++;}dfs(nextNum,target,list);}}
}

3.2 新思路

单看数组[1,1,5,6,7,10]。按照正常的递归思路。dfs(index,target)。
如果选择nums[index],则进入dfs(index+1,target-nums[index])状态。
如果不选择,则进入dfs(index+1,target)。
这里因为有重复数据,所以我们观察一下。如果target=7。
选择第0个1,和第3个6,可以得到结果。
选择第1个1,和第3个6,可以得到结果。但是重复了。说明第0个1不选择,那第1个1也不能选择。否则就重复了。这个也和第39有类似。不选择第0个1,选择第1个1是不可以的。

class Solution {private List<List<Integer>> answer;private int[] candidates;public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);this.candidates = candidates;answer = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();dfs(0,target,list);return answer;}private void dfs(int index,int target,List<Integer> list){if(target==0){answer.add(new ArrayList<Integer>(list));}else if(index>= candidates.length){return;}else{if(target<candidates[index]){return;}list.add(candidates[index]);dfs(index+1,target-candidates[index],list);list.remove(list.size()-1);while(index+1<candidates.length && candidates[index+1]==candidates[index]){index++;}dfs(index+1,target,list);}}
}

3.3 递归计数的方式

对于第39题,解法如下。对于第40题也一样。只是计数方式不同。

class Solution {private int[] candidates;private List<List<Integer>> answer;public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);this.candidates = candidates;answer = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();dfs(0,target,list);return answer;}private void dfs(int index,int target,List<Integer> list){if(target==0){answer.add(new ArrayList<Integer>(list));}else if(index>= candidates.length){return;}else{dfs(index+1,target,list);int maxCount = target/candidates[index];for(int i=1;i<=maxCount;i++){list.add(candidates[index]);dfs(index+1,target-i*candidates[index],list);}for(int i=1;i<=maxCount;i++){list.remove(list.size()-1);}}}
}

4 216. Combination Sum III

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
例子:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.

说明:1到9每个数字使用一次,每次取k个数,使得k个数之和等于n。

class Solution {private List<List<Integer>> answer;public List<List<Integer>> combinationSum3(int k, int n) {answer = new ArrayList<List<Integer>>();dfs(1,k,n,new ArrayList<Integer>());return answer;}private void dfs(int num,int k,int n,List<Integer> list){if(k==0){if(n==0){answer.add(new ArrayList<Integer>(list));}return;}if(num>9) return;if(num>n) return;for(int i=num;i<=9;i++){list.add(i);dfs(i+1,k-1,n-i,list);list.remove(list.size()-1);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424240.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第九十五期:Python帮你识破双11的套路

一年一度的“双十一”又要来了&#xff0c;很多人已经开始摩拳擦掌&#xff0c;毕竟几天之后手还在不在就不好说了。 作者&#xff1a;清风小筑 各种社交软件也是跟着遭殃&#xff0c;整天就是“来帮我一起盖楼”&#xff0c;各种字体绕过屏蔽&#xff0c;什么奇葩的脑洞也出来…

客户端独立弹出详细的实现过程

拷贝粘贴以下代码段&#xff0c;保存为html文件&#xff0c;试试看效果如何吧。。。前几天贴子没写东西就搁在上面搁了几天&#xff0c;真不好意思。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML><HEAD></HEAD>&…

Unity3D性能优化之Draw Call Batching

在屏幕上渲染物体&#xff0c;引擎需要发出一个绘制调用来访问图形API&#xff08;iOS系统中为OpenGL ES&#xff09;。每个绘制调用需要进行大量的工作来访问图形API&#xff0c;从而导致了CPU方面显著的性能开销。Unity在运行时可以将一些物体进行合并&#xff0c;从而用一个…

第九十六期:JavaScript 中的 4 个相等比较算法的介绍

JavaScript 运算中&#xff0c;一共包含 4 个相等比较算法&#xff1a;抽象相等比较&#xff1b;严格相等比较&#xff1b;SameValueZero&#xff1b;SameValue。 作者&#xff1a;zhangbao90s JavaScript 运算中&#xff0c;一共包含 4 个相等比较算法&#xff1a; 抽象相等…

VC6下使用WebLink控件

这是今年6月7号我的一篇日志&#xff0c;好像还有些参考价值&#xff0c;誊到blog上来吧。 最终我在VC6下面搞定了WebLink&#xff0c;与以往一样&#xff0c;论坛和MSDN是我最大的帮手。在ESRI官方论坛那可怜的四五个关于VC6WebLink的帖子中&#xff0c;我找到了困扰我多天的问…

建行B2B支付回调参数乱码现象解析

建行B2B支付采用Java开发&#xff0c;页面回调采用POST提交方式&#xff0c;编码为GBK。而我们的系统为ASP.NET&#xff0c;编码UTF-8。通过Request获取的参数是乱码&#xff0c;无奈之下&#xff0c;只能从InputStream解析。解析代码如下&#xff1a;log.Debug("Request …

77. Combinations

文章目录1 题目理解2 回溯1 题目理解 Given two integers n and k, return all possible combinations of k numbers out of 1 … n. You may return the answer in any order. 输入&#xff1a;两个int n和k。 规则&#xff1a;从1到n&#xff0c;n个数选择k个数&#xff0…

第九十七期:新版Kite:实时补全代码,Python之父都发声力挺!

不久前&#xff0c;一个免费的专门针对 Python 的代码补全工具 Kite&#xff0c;有了新的动态。 作者&#xff1a;杨鲤萍 本文转自雷锋网&#xff0c;如需转载请至雷锋网官网申请授权。 不久前&#xff0c;一个免费的专门针对 Python 的代码补全工具 Kite&#xff0c;有了新…

svn 版本升级的问题

原创文章&#xff0c;转载请注明 svn本地版本由1.6升级到1.7后&#xff0c;再使用时遇到一些问题&#xff0c;这里记录一下以备忘。 升级后&#xff0c;使用任何命令 不能用了&#xff0c;提示的意思大致是本地的workcopy版本太低了&#xff08;之前用1.6版本&#xff0c;check…

[JavaME]手机申请移动分配的动态IP(3)?

获取IP后是否可以和它通讯呢&#xff1f;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />Hi&#xff0c;继续上回的讨论《[JavaME]手机是否能够申请到动态IP[2]?》。 上回说到申请动态IP是可以了&#xff0c;并且准备好了一个线…

78. Subsets

文章目录1 题目理解2 回溯90 subset II方式一代码方式三代码1 题目理解 Given an integer array nums, return all possible subsets (the power set). The solution set must not contain duplicate subsets. 输入&#xff1a;int数组nums 输出&#xff1a;返回所有可能的子数…

第九十八期:TIOBE11月榜单:C、Swift、Go、D与Rust起起伏伏

TIOBE 指数并不代表语言的好坏&#xff0c;开发者可以使用该榜单检查自身的编程技能是否需要更新&#xff0c;或者在开始构建新软件时对某一语言做出选择。 作者&#xff1a;oschina TIOBE 公布了 11 月份编程语言排行榜。 本月前 20 名中有一些有趣的现象&#xff0c;先看看…

Vue 之 slot(插槽)

前言&#xff1a; vue中关于插槽的文档说明很短&#xff0c;语言又写的很凝练&#xff0c;再加上其和methods&#xff0c;data&#xff0c;computed等常用选项在使用频率、使用先后上的差别&#xff0c;这就有可能造成初次接触插槽的开发者容易产生“算了吧&#xff0c;回头再学…

sharepoint安装心得_过程

sharepoint安装心得_过程 我是新手,没有接触过sharepoint以前,所有在安装方面吃了一些亏 下面说一下正确的顺序: windows 2003 域服务器(ad) vs.net(如果需要的话,但不要装windows的frontpage的扩展,虽然.net需要,不装亦可) sqlserver 2000(sp3) sharepoint server 2003卸载顺序…

46. Permutations

文章目录1题目理解2 回溯3 47. Permutations II1题目理解 Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. 输入&#xff1a;整数数组nums&#xff0c;所有元素不相同 输出&#xff1a;数组的所有…

第九十九期:可以编写代码的代码:代码生成的利与弊

代码生成的当前状态是无处不在的&#xff08;2019年春季&#xff09;。REST API的数量激增&#xff0c;导致在过去十年中针对各种编程语言和环境开发的API客户端生成器种类繁多。 作者&#xff1a;约翰麦克马洪 代码生成的当前状态 代码生成的当前状态是无处不在的&#xff0…

TD商用将迈重要一步 六大运营商年底建网试验

TD商用将迈重要一步 六大运营商年底建网试验 中国电信、中国网通、中国移动、中国联通、中国铁通和中国卫通同时开动 TD-SCDMA独立组网实验即将迈出重要一步。 昨天记者从知情人士处获悉&#xff0c;如无意外&#xff0c;参加信产部组织的TD-SCDMA外场第三阶段测试的系统厂商将…

天气预报的Ajax效果

最近在网站上看了很多显示实时天气预报的&#xff0c;挺实用而且用户体验也不错。对用户的帮助也比较大&#xff0c;用户可以通过你的网站了解到实时的天气信息。感觉比较有意思&#xff0c;于是自己钻研了一下其中的实现方法。于是决定把代码分享给大家&#xff0c;希望能对大…

784. Letter Case Permutation

文章目录1 题目理解2 回溯1 题目理解 Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create. You can return the output in any order. 输入&…

第一百期:Java架构师:高并发下的流量控制

这个时候如果不做任何保护措施&#xff0c;服务器就会承受很大的处理压力&#xff0c;请求量很高&#xff0c;服务器负载也很高&#xff0c;并且当请求超过服务器承载极限的时候&#xff0c;系统就会崩溃&#xff0c;导致所有人都不能访问。 作者&#xff1a;IT技术分享 这个…