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 ...
随机推荐
- CloudSim学习
CloudSim CloudSim是墨尔本大学云计算和分布式系统实验室推出的云计算模拟软件.它可以使研究者规避实际部署的诸多不便(比如说资金缺乏等因素),在单机上即可实现对大规模云集群的模拟和相应算法 ...
- Spring Boot教程(四)接收上传的multi-file的文件
构建工程 为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖.为例能够上传文件在服务器,你需 ...
- Monkeyrunner自动化测试由浅入深(第一节)
(原版)Monkeyrunner自动化测试由浅入深(第一节) 博主原创,请勿转载 第一.相关软件和环境的配置 1.Android sdk下载和配置 2.java jdk下载和配置 第二.Monkeyr ...
- [CSP-S模拟测试]:maze(二分答案+最短路)
题目传送门(内部题88) 输入格式 第一行两个数$n,m$.第二行四个数$sx,sy,tx,ty$.分别表示起点所在行数.列数,终点所在行数.列数.接下来$n$行,每行$m$个数,描述迷宫.最后一行一 ...
- socket通信(TCP和UDP)
1.TCP 2.UDP
- [NLP] nlp-lstm-cos -> sin
LSTM 看了官方lstm以及相关原理,然后自己按照理解写了一遍,然后在网上看到cos预测sin问题,然后用lstm完成了建模. 看到好多论文里图像文本特征用lstm的,对学ocr有点帮助. 官方ls ...
- Unity ZTest 深度测试 & ZWrite 深度写入
初学Shader,一开始对于渲染队列,ZTest 和 ZWrite一头雾水,经过多方查阅和实验,有了一些自己的理解.发此文与初学Shader的朋友分享,也算是为自己做个笔记.不对或不足之处欢迎指正. ...
- C 语言的函数 - 内存分析
函数基本概念 Linux 中,函数在内存的代码段(code 区),地址比较靠前. 函数定义 C 语言中,函数有三个要素:入参.返回值.函数名,缺一不可.函数使用前必须先声明,或者在使用之前定义. 函数 ...
- xshell 缺少mfc110u.dll
https://www.microsoft.com/zh-CN/download/details.aspx?id=30679 如果 x64 没有反应,就下载 x86 安装试试,
- Linux 下创建静态库和动态库
1.创建静态链接库 2.创建动态链接库