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 ...
随机推荐
- [CSP-S模拟测试]:数列(数学)
题目传送门(内部题95) 输入格式 第一行三个整数$n,a,b$,第二行$n$个整数$x_1\sim x_n$表示数列. 输出格式 一行一个整数表示答案.无解输出$-1$. 样例 样例输入:2 2 3 ...
- 【Python】学习笔记五:缩进与选择
Python最具特色的用缩进来标明成块的代码 缩进 i = 4 j = 2 if i > j: i = i+1 print(i) 这是一个简单的判断,Python的if使用很简单,没有括号等繁琐 ...
- 2018-2019-2-20175225 实验四《Android开发基础》实验报告
一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:张元瑞 学号:20175225 指导教师:娄嘉鹏 实验日期:2019年5月14日 实验时间:13:45 - 21:00 实验序号:实验 ...
- VC CString,int,string,char*之间的转换
CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...
- Flask基础以及Response三剑客
Flask的特点: 优点:小而精.三方组件全 缺点: 性能相对较差 因为依赖三方组件所以在更新的时候难免不同步 基础模板 from flask import Flask app = Flas ...
- Text Classification
Text Classification For purpose of word embedding extrinsic evaluation, especially downstream task. ...
- Quartz快速上手
快速上手你需要干啥: 下载Quartz 安装Quartz 根据你的需要来配置Quartz 开始一个示例应用 下载和安装 The Quartz JAR Files The main Quartz lib ...
- K8S 笔记,请坚持
service 通过 selector 来选择指定 label 的 pod. 如何访问 service 呢?集群内部访问,使用 cluster ip:集群外部访问,使用 NodePort. clust ...
- rocketMQ 通信协议格式
rocketMQ 使用 netty 通信,端对端的通信,为了避免粘包.分包,需要指定发送数据的边界. 使用的解码器是 LengthFieldBasedFrameDecoder // org.apach ...
- 自建 CA 中心并签发 CA 证书
目录 文章目录 目录 CA 认证原理浅析 基本概念 PKI CA 认证中心(证书签发) X.509 标准 证书 证书的签发过程 自建 CA 签发证书并认证 HTTPS 网站的过程 使用 OpenSSL ...