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 ...
随机推荐
- [CSP-S模拟测试]:123567(莫比乌斯函数+杜教筛+数论分块)
题目传送门(内部题92) 输入格式 一个整数$n$. 输出格式 一个答案$ans$. 样例 样例输入: 样例输出: 数据范围与提示 对于$20\%$的数据,$n\leqslant 10^6$. 对于$ ...
- 交互式数据可视化-D3.js(二)选择集和数据
选择集 select和selectAll类似jquery: d3.select('body') d3.select('.body') d3.select('#body') d3.selectAll(' ...
- Servlet的常见错误
Servlet常见的错误: 1.404错误:资源未找到 原因一:在请求地址中的servlet的别名书写错误. 原因二:虚拟机项目名称拼写错误. 2.500错误:内部服务器错误 错误一: java.la ...
- Spring下面的@Transactional注解的讲解
摘自: https://www.cnblogs.com/xiohao/p/4808088.html Spring下面的@Transactional注解标志的讲解 最近在开发中对Spring中的事务标记 ...
- Elasticsearch Java Rest Client API 整理总结 (一)
http://www.likecs.com/default/index/show?id=39549
- ffmpeg转码指南
Windows下面的安装: 浏览器打开https://ffmpeg.zeranoe.com/builds/ 选择好版本,位数如果不确定的话就下载32bit的,然后选择static,然后下载 下载完成后 ...
- 在vi vim中使用正则表达式与 普通perl正则的区别?
参考这篇文章很好 vim中的正则表达式常用的命令有种, 即搜索和替换 /: 搜索 :s 替换 在vim中的正则表达式和perl编程的正则表达式还是有区别的: 正则表达式中的内容包括: 字面字符... ...
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_2 连接池介绍
- 测开之路一百四十八:WTForms表单验证
使用WTForms表单验证,可以在数据建模时就设置验证信息和错误提示 创建模型时,设置验证内容,如必填.格式.长度 from flask_wtf import Formfrom wtforms imp ...
- 【FICO系列】SAP FICO 凭证错误:BKPFF$PRDCLN800在FI中达到的项目最大编号
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO 凭证错误:BK ...