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- ...
随机推荐
- 比较R语言、perl语言,matlab中for循环和while循环的使用
http://zhan.renren.com/zxccshkbx?gid=3602888498030523562&from=post&checked=true
- ubuntu下配置.net core运行环境
Ubuntu 16.4执行: sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotne ...
- 【VBA研究】利用DateAdd函数取上月或上年同期的日期
作者:iamlaosong DateAdd函数返回一个日期.这一日期加上了一个时间间隔.通过这个函数能够计算非常多我们须要的日期,比方上月上年同期日期等. 语法 DateAdd(interval, n ...
- H5 获取地理位置
只能通过手机浏览器访问,并且用户必须允许访问才可以生效 <!doctype html> <html> <head> <title>Mobile Cook ...
- centos 中GTK的安装
centos 中GTK的安装 yum install gtk*
- Post+Get方式接口测试代码编写
详细代码如下 package testproject; import java.io.BufferedReader; import java.io.IOException; import java.i ...
- javascript 函数声明和函数表达式
定义js函数的方法有两种,1.函数声明 2.函数表达式 这两种方式的区别是:1.函数声明可以先调用后定义(javascript引擎在解释的时候会把所有的函数声明提升)2.函数表达式必须先定义后使用.看 ...
- 一图总结C++中关于指针的那些事
指向对象的指针.指向数据成员的指针,指向成员函数的指针: 数组即指针,数组的指针,指针数组: 指向函数的指针,指向类的成员函数的指针,指针作为函数參数,指针函数: 指针的指针,指向数组的指针:常指针. ...
- 启动storm之后浏览器访问报错,org.apache.thrift7.transport.TTransportException: java.net.ConnectException: Connection refused (Connection refused)
原因是zookeeper没有启动 Internal Server Error org.apache.thrift7.transport.TTransportException: java.net.Co ...
- Storm实战
需求: spout输出一些手机品牌小写名称,第一个bolt将手机名称转成大写,第二个bolt在手机名称的后面再追加上时间. 项目目录: 导入相关的jar包. RandomWordSpout.java: ...