39. Combination Sum (Java)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[2,3,6,7],target =7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5],target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> ans = new ArrayList<Integer>();
Arrays.sort(candidates);
backTrack(candidates, target, 0, ans, 0);
return result;
}
public void backTrack(int[] candidates, int target, int start, List<Integer> ans, int sum){
if(start >= candidates.length || sum + candidates[start] > target)
return; //not found
else if(sum + candidates[start] == target ){ //found an answer
List<Integer> new_ans = new ArrayList<Integer>(ans); //不能用List<Integer> new_ans = ans;这个只是创建了原List的一个引用
new_ans.add(candidates[start]);
result.add(new_ans);
}
else{
// not choose current candidate
backTrack(candidates,target,start+1,ans,sum);
//choose current candidate
ans.add(candidates[start]);
backTrack(candidates,target,start,ans,sum+candidates[start]);
ans.remove(ans.size()-1); //List是按引用传递,为了不影响递归,需要复原
}
}
private List<List<Integer>> result = new ArrayList<List<Integer>>();
}
注意List按引用(地址)传递,需要新new一个,或是复原,以不影响其他部分
39. Combination Sum (Java)的更多相关文章
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- arguments详解——函数内命名参数之映射
首先,arguments对象是所有(非箭头)函数中都可用的局部变量.你可以使用arguments对象在函数中引用函数的参数.此对象包含传递给函数的每个参数,第一个参数在索引0处. arguments对 ...
- wannalfy 挑战赛8 F 白云的树(树形dp)
链接:https://www.nowcoder.com/acm/contest/57/F 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524288K 64b ...
- [UPC10525]:Dove打扑克(暴力+模拟)
题目描述 $Dove$和$Cicada$是好朋友,他们经常在一起打扑克来消遣时光,但是他们打的扑克有不同的玩法. 最开始时,牌桌上会有$n$个牌堆,每个牌堆有且仅有一张牌,第$i$个牌堆里里里那个扑克 ...
- 深入理解php的输出缓冲区(output buffer)
这篇文章是翻译自Julien Pauli的博客文章PHP output buffer in deep,Julien是PHP源码的资深开发和维护人员.这篇文章从多个方面讲解了PHP中的输出缓冲区以及怎么 ...
- tensorflow源码分析——CTC
CTC是2006年的论文Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurren ...
- 一、Jmeter启动报错:Could not initialize class org.apache.jmeter.gui.util.MenuFactory
1.下载: plugins-manager.jar 包 2.地址:https://jmeter-plugins.org/install/Install/ 3.将jar包放到lib/ext 4.重启jm ...
- 【MAC】 命令行解压缩 rar 文件
使用Mac解压rar文件很简单,总共分2步.step1:安装解压工具,step2:解压 1.使用Homebrew安装unrar brew install unrar 2.cd到rar文件的目录下,然后 ...
- await Vue.nextTick() 的含义分析
概述 今天看别人的单元测试代码的时候碰到了一段代码 await Vue.nextTick(),初看起来不是很懂,后来通过查资料弄懂了,记录下来,供以后开发时参考,相信对其他人也有用. await Vu ...
- NumericStream && Stream综合练习
一.NumericStream 我们可以将一个Stream转化为对应的数字Stream,如mapToInt.mapToLong转化为IntStream.LongStream等(NumericStrea ...
- BFC块级格式
BFC块级格式上下文,独立的一个渲染区域 1.同一个BFC的两个相邻盒子间的margin会重叠(垂直方向): 2.BFC内部的盒子在垂直方向上会一个接一个的放置: 3.每个子元素的左外边距与包含块的左 ...