Combination Sum II —— LeetCode
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is: [1, 7] [1, 2, 5] [2, 6] [1, 1, 6]
题目大意:跟上一题类似,但是有点区别就是候选集中的元素只能出现一次。
解题思路:每次从当前元素的下一个开始计算sum,并把candidate加入List,并且跳过重复元素。
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
Deque<Integer> tmp = new ArrayDeque<>();
Arrays.sort(candidates);
helper(res, tmp, 0, target, candidates);
return res;
}
private void helper(List<List<Integer>> res, Deque<Integer> tmp, int start, int target, int[] candidate) {
if (target == 0) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i < candidate.length && target >= candidate[i]; i++) {
tmp.addLast(candidate[i]);
helper(res, tmp, i + 1, target - candidate[i], candidate);
tmp.removeLast();
while (i < candidate.length - 1 && candidate[i + 1] == candidate[i]) {
i++;
}
}
}
Combination Sum II —— LeetCode的更多相关文章
- Combination Sum II leetcode java
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- Combination Sum II [LeetCode]
Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...
- [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解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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 ...
随机推荐
- 层叠样式优先级CSS
按照W3School网站(点这里直达)的说法,当同一个 HTML 元素被不止一个样式定义时,它们是有优先级之分的,如下,将优先级从小到大排列出来,其中4的优先级最高: 1.浏览器缺省设置2.外部样式表 ...
- Asp.Net MVC安全更新MS14-059导致项目编译失败
微软最近一次安全更新MS14-059(链接:https://technet.microsoft.com/en-us/library/security/ms14-059)由于直接应用到了machine. ...
- linux 脚本编写基础(一)
1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...
- 受限玻尔兹曼机(RBM)
能量模型 RBM用到了能量模型. 简单的概括一下能量模型.假设一个孤立系统(总能量$E$一定,粒子个数$N$一定),温度恒定为1,每个粒子有$m$个可能的状态,每个状态对应一个能量$e_i$.那么,在 ...
- c#中设置按钮Button为透明
方法一:代码实现 /// <summary> /// 设置透明按钮样式 /// </summary> private void SetBtnStyle(Button btn) ...
- 颜色rgb
1.几种基本颜色的rgb 黑色:R.G.B(0.0.0) 白色:R.G.B(255.255.255) 红色:R.G.B(255.0.0) 绿色:R.G.B(0.255.0) 蓝色:R.G.B(0.0. ...
- 文字超出DIV的边框
已经给div设置了高宽,但是文字还是会戳出div而不是换行 鼓捣了一下好像是因为这个原因 如果全是 aaaaaaaaaaaaaaaaaaaaa 这样的纯英文,那么测试的时候是不会换行的,因为浏览器认为 ...
- 【转】jQuery教程
“jQuery风暴” 推荐及配套代码下载 ziqiu.zhang 2011-03-24 00:28 阅读:15339 评论:100 从零开始学习jQuery(剧场版) 你必须知道的javascri ...
- 【POJ3580】【块状链表】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- 数组操作- reverse sort each 操作
reverse reverse 操作符会读取列表(也可能来自数组),并按相反的次序返回该列表. .. ; @barney = reverse(@fred); # 得10,9,8,7,6 .. ; # ...