40. Combination Sum II (JAVA)
Given a collection of candidate numbers (candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
Each number in candidates
may only be used once in the combination.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[10,1,2,7,6,1,5]
, target =8
,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
与Combination Sum的区别在于,本题每次递归需要考虑重复元素,用while循环递归重复元素出现的次数;而Combination Sum每次递归只需要考虑两种情况,即放入该元素,或不放入该元素。
class Solution {
public List<List<Integer>> combinationSum2(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(sum == target ){ //found an answer
List<Integer> new_ans = new ArrayList<Integer>(ans); //不能用List<Integer> new_ans = ans;这个只是创建了原List的一个引用
result.add(new_ans);
}
else if(start >= candidates.length || sum > target)
return; //not found
else{
int cnt = 0; //repeated times
while(start+1 < candidates.length && candidates[start+1]==candidates[start]){
start++;
cnt++;
}
// not choose current candidate
backTrack(candidates,target,start+1,ans,sum); //choose current candidate
List<Integer> backup = new ArrayList<Integer>(ans);
int i = 0;
for(i = 0; i <= cnt && sum <= target;i++){
backup.add(candidates[start]);
sum += candidates[start];
backTrack(candidates,target,start+1,backup,sum);
}
}
} private List<List<Integer>> result = new ArrayList<List<Integer>>();
}
40. Combination Sum II (JAVA)的更多相关文章
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- CSS 阴影应用
1.背景阴影的使用 box-shadow:1px 1px 1px 1px #ccccccc inset; 说明:第一个参数为水平阴影的大小.第二个为垂直阴影的大小(值可以为负数).第三个参数是阴影模糊 ...
- 【商业智能VS人工智能】
什么是智能? 从感觉到记忆到思维这一过程,称为“智慧”,智慧的结果就产生了行为和语言,将行为和语言的表达过程称为“能力”,两者合称“智能”,将感觉.去记.回忆.思维.语言.行为的整个过程称为智能过程, ...
- 使用私有仓库(Docker Registry 2.0)管理镜像
1. 执行以下命令新建并启动一个Docker Registry 2.0 docker run -d -p 5000:5000 --restart=always --name registry2 reg ...
- XDebug安装配置教程
笔者的开发环境如下:Windows8.1+Apache+PhpStorm+XDebug+Firefox(XDebug helper 1.4.3插件). 转载http://www.jb51.net/ar ...
- 2014过去了,正式步入职场了,.net
一.第一家公司(北京XXXXXXX) 从2014年7月1号拿到学位证,到7月15号到北京,努力找工作,用了两个多礼拜,终于找到了一个只有三个人的公司,愿意要我,薪资是实习三千,转正四千. 2014年7 ...
- spring hibernate 事务整合 使用测试类 事务不自动提交的问题!!!
使用JUnit 测试hibernate 事务管理的时候应注意 ,测试类完成是默认回滚的. 所以只能查询数据库却不能增删改数据库. 应该在测试类上面加上注解 @Rollback(false) 表似默认 ...
- 宝塔面板修改用户名和密码报错:TypeError: cannot concatenate 'str' and 'NoneType' objects
[root@dapao~]# bt 14 正在执行(14)... ================================================================== ...
- windows,oracle,dg报错:ORA-12528,ORA-12154,ORA-10456 ,PING[ARC1]: Heartbeat failed to connect to standby 'orclbk'. Error is 12154
windows,oracle,dg报错:ORA-12528,ORA-12154,ORA-10456 最近有需求在windows的2台oracle服务器上搭建dg,在过程中遇到了一些错误,跟在linux ...
- spring boot 异常汇总
spring boot JPA 异常: org.springframework.data.mapping.PropertyReferenceException: No property role fo ...
- ESP32 Ethernet to wifi
参考网址 https://github.com/espressif/esp-iot-solution/tree/master/examples/eth2wifi RMII PHY Wiring(RMI ...