[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 ...
随机推荐
- h5与安卓、ios交互
1.安卓交互 h5调用安卓方法 window.webview.xxx() 安卓调用h5方法, 方法需要在全局注册 window['showUnreadMsg'] = () => { this.$ ...
- 18 11 04 初用单片机 c语言学习
---恢复内容开始--- 1 作为单片机使用的的 c 语言学习 ++ 增位运算符 在原有基础上加一 -- 相同 由于单片机只有 ~ 取反 & 两个 参数里有没有 | 两个 参数里有没有 ^ 两 ...
- CSS3-选中的锚链接改变指定样式
1.css样式 主角就是:target <a> 标签的 target 属性规定在何处打开链接文档,连接文档也可以是一个样式. 如果在一个 <a> 标签内包含一个 target ...
- BZOJ 4913 [Sdoi2017] 遗忘的集合
骂了隔壁的 BZOJ垃圾评测机 我他妈卡了两页的常数了 我们机房的电脑跑的都比BZOJ快
- LINUX之ntp时间同步服务配置
本篇将介绍LINUX之ntp服务配置,时钟同步服务器配置.这个在很多地方都会用到,保持各主机之前的时间保持一致,保证主机之间的心跳稳定. 三台主机都是centos7 192.168.1.110 mas ...
- 计算机网络(2): http的基础上用SSL或TSL加密
加密过程具体TCP实现 步骤 1 : 客户端通过发送Client Hello报文开始SSL通信(这里是在TCP的三次握手已经完成的基础上进行的).报文中包含客户端支持的SSL的指定版本.加密组件列表( ...
- 项目常用JS方法封装--奋斗的IT青年(微信公众号)
...
- Jisa's Notebook
同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- 微信小程序2048开发进度(一)
父亲是个体劳动者,他的兴趣就是下象棋,针对平时兴趣,我想做一款自己的2048小游戏,在慕课网观看了2048小游戏的讲解,以及关于开发小游戏的理论知识,对开发一款小游戏有了基本的认识.