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 ...
随机推荐
- hadoop集群环境搭建准备工作
一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...
- jetty运行maven程序(修改及时生效,不需要重启jetty程序)
jetty:run -Djetty:port=9999
- Linux运行C#程序
首先需要安装mono 安装教程http://www.cnblogs.com/aixunsoft/p/3422099.html 然后 用终端执行C#程序就可以了,mono 程序文件名 可以直接执行win ...
- oracle数组学习资料
--oracle数组,所谓数组就是 字段的 个数,数组应该很有用 --可变数组 declare type v_ar is varray(10) of varchar2(30); my_ar v ...
- 最简单的基于FFmpeg的移动端例子:IOS 视频转码器
===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...
- 英语学习[ZZ]
本文作者三年间从四级勉强及格到高级口译笔试210,口试232.找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考了.虽不敢说方法一定是最好的,但从现在开始随便谁不要再去找学习资料,每天花两个钟 ...
- hdu2962 Trucking (最短路+二分查找)
Problem Description A certain local trucking company would like to transport some goods on a cargo t ...
- linux负载均衡
1.linux lvs nat实现负载均衡 添加两块网卡并开启路由管道 > /proc/sys/net/ipv4/ip_forward //开始路由管道 安装ipvsadm yum instal ...
- request.getSession()
request.getSession(); 与request.getSession(false);区别 服务器把session信息发送给浏览器 浏览器会将session信息存入本地cookie中 ...
- linux定时器
我们常常有设置系统在某一时间执行相应动作的需求,比如设置电脑什么时候自动锁屏,什么时候自动关机,设置应用程序什么时候自动运行,什么时候自动退出.这些与时间相关的功能,都需要依靠操作系统中的定时器来实现 ...