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 ...
随机推荐
- Selenium 定位一组元素
到目前为止,我们已经知道了怎么去定位一个元素,有时候我们需要获取页面上的多个元素,该怎么办呢?难道还一个一个地去定位,那也太lower了吧. webdriver提供了定位一组元素的方法,跟前面讲的那八 ...
- (76)深入浅出Mqtt协议
物联网(Internet of Things,IoT)时代机器之间(Machine-to-Machine,M2M)的大规模沟通需要发布/订阅(Publish/Subscribe)模式,轻量级.易扩展的 ...
- js for循环中i++与++i有什么区别
平时都是这样写的for循环, 1 2 3 for(var i = 0; i < 20 ; i++){ .... } 但我看有的人这样写 for (var i = 0; ...
- (四)mysql -- 常用函数
今天get一个,先记录一下 以后慢慢补充~ 将varchar转换成int 例如:select * from tb_1 order by cast(sport_sum as unsigned integ ...
- WebApi系列知识总结
WebApi系列知识 一.webApi项目搭建 1.新建WebApi项目 (1) (2) (3) (4) Areas – HelpPage – App_Start – HelpPageConfig.c ...
- Mybaits基本的CURD操作
1 首先在Mapper.xml配置 <!-- parameterType:参数类型,可以省略, 获取自增主键的值: mysql支持自增主键,自增主键值的获取,mybatis也是利用stateme ...
- VGA接口一根针折了
注意!!要由 针 对照着 接口 看!!别看反了! VGA接头图如下: VGA接口,15根针,其对应接口定义如下: 1红基色 red 2 绿基色 green 3 蓝基色 blue 4 地址码 ID Bi ...
- eclipse有时候导入了包,但是还是有红线,找不到
clean一下工程,选择project->clean->clean project selected blow并且选择build only the selected project
- 安装 Windows 系统在 NVMe 规范的 M.2 接口的固态硬盘(SSD)上
作为一个程序员很重要的一项技能就是装系统 @_@,以前我都是随便用网上的工具做个系统盘,每次要用直接随手就搞好了,节省大家时间. 但最近同事装了个贼小的固态,然后我启动盘里的系统果断识别不出来他的固态 ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_2 Mybatis的CRUD-保存操作
增加的方法 修改映射配置 id是方法名 按照原来jdbc的写法 values里面应该是一堆问号,现在这里不能再去写问号了因为要取值 从我们要执行的方法传的参数里面去取值 所以参数的类型我们必须要告诉这 ...