[LC] 40. Combination Sum II
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]
] Time: O(2^N)
Space: O(N)
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
Arrays.sort(candidates);
List<Integer> lst = new ArrayList<>();
helper(res, lst, candidates, target, 0);
return res;
} private void helper(List<List<Integer>> res, List<Integer> lst, int[] candidates, int reminder, int index) {
if (reminder < 0) {
return;
}
if (reminder == 0) {
res.add(new ArrayList<>(lst));
}
for (int i = index; i < candidates.length; i++) {
//i > index will only skip when numbers before cur is used.
// i > 0 skip all one combination like [1, 1, 6]
if(i > index && candidates[i] == candidates[i - 1]) {
continue;
}
lst.add(candidates[i]);
helper(res, lst, candidates, reminder - candidates[i], i + 1);
lst.remove(lst.size() - 1);
}
}
}
[LC] 40. Combination Sum II的更多相关文章
- [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] 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
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 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
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- centos7-vsftp(虚拟用户)
要求如下: 1.所有用户主目录为/var/www宿主为virtual用户: 2.ftpuser1用户只能下载不能上传以及删除文件重命名操作: 3.ftpuser2可以下载与上传文件以及删除重命名操作: ...
- 寒假day16
今天优化了管理员界面,人才标签模块遇到了一点问题,部分结果无法显示,正在寻找原因
- 计算机utf-8/gbk/utf-16对照表
GBK UTF-16 UTF-8 ==================D2BB 4E00 E4 B8 80 一B6A1 4E01 E4 B8 81 丁C6DF 4E03 E4 B8 ...
- redis在linux中的安装启动
1. 拖到 /usr/local 下 2. 解压 tar zxf redis-4.0.8.tar.gz 3. mkdir /usr/redis 4. 编译 cd redis-4.0.8/src ...
- SDL实践:产品经理如何驱动产品安全建设
一.序言 本文从产品经理的角度出发,对产品经理的安全职责.产品驱动安全的内涵.工作内容.工作方法.所需安全资源.以及产品经理的安全工作量进行了分析.希望所有产品经理在没有心理负担的情况下,有目标.有方 ...
- flutter依赖某些插件,点击运行会出现错误
org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true android.enableAapt ...
- centos 部署WGCLOUD
服务端: 一.安装jdk 1.查看安装配置 jdk: 命令:java -version 2.查看系统是否自带 jdk rpm -qa |grep java rpm -qa |grep jdk rp ...
- 使用Anaconda安装TensorFlow
conda create -n tensorflow python=2.7 # or python=3.3, etc. pip install --ignore-installed --upgrade ...
- c#为什么要用事物
一.事务的定义 所谓事务,它是一个操作集合,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.典型的例子就像从网上银行系统的帐户A转帐到帐户B,它经过两个阶段:1.从帐户A取出款项.2.把 ...
- typescript-学习使用ts-1
Hello World 新建 greeter.ts 并写入以下内容: function greeter(person) { return "Hello, " + person; } ...