[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 ...
随机推荐
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- Codeforces Round #620 (Div. 2)E LCA
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...
- 修复grub
进入命令行模式,#chroot /mnt/sysimage :切换根目录#grub2-install /dev/sda :安装grub2到第一硬盘#grub2-mkconfig -o /boot/gr ...
- TCP/IP通信过程
一.参考网址 1.以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 2. 二.TCP的建立过程 1.例子: 192.168.22.66 telenet到192.168.22.74的tcp ...
- 201503-2 数字排序 Java
思路: 将出现过的数以及次数放进Map中,转成List,用Comparator就可以排序了.参数中o2-o1,与排序规则相反,为降序 import java.util.ArrayList; impor ...
- 提高WiFi上网速度
https://jingyan.baidu.com/article/1876c852aa668c890b1376c4.html http://www.coozhi.com/youxishuma/you ...
- 吴裕雄--天生自然 PHP开发学习:函数
<?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; wr ...
- jquery.marquee.js - 有点奇怪的跑马灯动画,不过还是加上去了
客户想要一个跑马灯的效果,最终我用了jquery.marquee.js. 这个库很简单就能用. 效果是这样,从左到右,移动速度都不一样. 1. HTML <div class="mar ...
- 使用python列出目录下的所有文件
https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-pyth ...
- 吴裕雄--天生自然 PYTHON3开发学习:字典
dict = {'} dict1 = { 'abc': 456 } dict2 = { 'abc': 123, 98.6: 37 } dict = {'Name': 'Runoob', 'Age': ...