Leetcode-Combinations Sum II
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]
public class Solution {
public List<List<Integer>> combinationSum2(int[] num, int target) {
int[] candidates = num;
List<List<Integer>> resSet = new ArrayList<List<Integer>>();
List<Integer> curRes = new ArrayList<Integer>();
if (candidates.length==0) return resSet;
Arrays.sort(candidates);
int cur=0,end=candidates.length-1;
for (int i=0;i<candidates.length;i++)
if (candidates[i]>target){
end = i-1;
break;
} sumRecur(candidates,cur,end,target,resSet,curRes); return resSet; } public void sumRecur(int[] candidates, int cur, int end, int valLeft, List<List<Integer>> resSet, List<Integer> curRes){
if (valLeft==0){
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(curRes);
resSet.add(temp);
return;
} if (cur>end) return; int newLeft = valLeft;
int curLen = curRes.size();
int nextIndex = cur;
while (nextIndex<=end && candidates[nextIndex]==candidates[cur]) nextIndex++; for (int i=cur;i<nextIndex;i++)
if (newLeft>=candidates[i]){
curRes.add(candidates[i]);
newLeft -= candidates[i];
sumRecur(candidates,nextIndex,end,newLeft,resSet,curRes);
} else
break; while (curRes.size()!=curLen) curRes.remove(curRes.size()-1); sumRecur(candidates,nextIndex,end,valLeft,resSet,curRes);
}
}
Leetcode-Combinations Sum II的更多相关文章
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Two Sum II - Input array is sorted
原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [leetcode]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
随机推荐
- Ubuntu 安装配置 JDK+Tomcat+Nginx
安装配置JDK 下载安装 # 下载: wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=acc ...
- java基础讲解03-----java的结构
前面我们说了java是面向对象的语言,java程序的基本组成单元是类,类中又属性,方法两个部分,每个应用程序都会有一个mian函数,含有main()方法的类,我们称为主类 package Test; ...
- python selenium ---键盘事件
转自:http://www.cnblogs.com/fnng/p/3258946.html 本节重点: l 键盘按键用法 l 键盘组合键用法 l send_keys() 输入中文运行报错问题 键盘按键 ...
- Android:实现手势滑动的事件处理方法
首先得Activity必须实现OnGestureListener接口,该接口提供了关于手势操作的一些方法, onDown方法:onDown是,一旦触摸屏按下,就马上产生onDown事件 ...
- 几种TCP连接中出现RST的情况(转载)
TCP RST 网络 linux 目录[-] 1 端口未打开 2 请求超时 3 提前关闭 4 在一个已关闭的socket上收到数据 总结 参考文献: 应该没有人会质疑,现在是一个网络时代了.应该不少程 ...
- 实验c语言不同类型的指针互用(不推荐只是学习用)
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, world\n"); ]; i ...
- Java并发编程(四):线程安全性
一.定义 当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程安全的. 二.线程安 ...
- 为每个页面加上Session判断
首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下: using System; using System.Data; using System.Configura ...
- 关于Animator获取当前剪辑长度
通常下意识的肯定用这个接口 GetCurrentAnimatorStateInfo().length 但是存在一个过渡动画的问题,具体看这篇:过渡动画的测试 所以当播新的状态时直接取动画时间,取到的就 ...
- [svc][op]pip安装ansible && yum安装python34
相对yum安装,pip安装的好处是jinjia版本到了2.8 pip安装ansible Successfully installed MarkupSafe-1.0 PyYAML-3.12 ansibl ...