LeetCode——Combination 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]
原题链接:https://oj.leetcode.com/problems/combination-sum-ii/
与之前一题的差别在于每一个数字仅仅能使用一次。
故需在递归的时候直接使用下一个数字。
public class CombinationSumII {
public static void main(String[] args) {
List<List<Integer>> list = new CombinationSumII().combinationSum2(new int[]{10,1,2,7,6,1,5},8);
for(List<Integer> li : list){
for(Integer i : li)
System.out.print(i + "\t");
System.out.println();
}
}
private List<Integer> list = new ArrayList<Integer>();
private List<List<Integer>> result = new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum2(int[] num, int target) {
if(num.length == 0)
return result;
Arrays.sort(num);
dfs(num,target,0);
return result;
}
public void dfs(int[] candidates,int target,int index){
if(target < 0)
return;
if(target == 0){
result.add(new ArrayList<Integer>(list));
return;
}
for(int i=index;i<candidates.length;i++){
if(i>index && candidates[i] == candidates[i-1])
continue;
list.add(candidates[i]);
dfs(candidates,target-candidates[i],i+1);
list.remove(list.size()-1);
}
}
}
LeetCode——Combination 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] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [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 ...
随机推荐
- Windows7安装CodeTyphon
WARNING 1: On windows (XP, Vista and Win7), you must install this software as user with administrato ...
- [leetcode]Gas Station @ Python
原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...
- SendMessage消息大全及说明
WM_CREATE = &H0001 应用程序创建一个窗口 WM_DESTROY = &H0002 一个窗口被销毁 WM_MOVE ...
- Pearson(皮尔逊)相关系数
Pearson(皮尔逊)相关系数:也叫pearson积差相关系数.衡量两个连续变量之间的线性相关程度. 当两个变量都是正态连续变量,而且两者之间呈线性关系时,表现这两个变量之间相关程度用积差相关系数, ...
- jedis 连接 redis:Could not get a resource from the pool——我的出错原因和解决办法
windows 下安装的,本机使用 现象:刚装好开发使用好好的, 重启电脑后就报这个错 网上的所有可能都试过,没有用. 最后,放弃所有包装,用最原始的代码进行连接测试: Jedis jedis=new ...
- linux ps 命令的查看
linux ps 命令的结果中VSZ,RSS,STAT的含义和大小 ps是linux系统的进程管理工具,相当于windows中的资源管理器的一部分功能. 一般来说,ps aux命令执行结果的几个列的信 ...
- C++ 函数适配器
1.考虑下面的需求,在一个int的vector中,找出一个比5的元素,容易想到的解决办法,定义一个方法对象,使用模板,如下:vector<int>::iterator iter = fin ...
- Oracle导入excel数据快速方法
Oracle导入excel数据快速方法 使用PLSQL Developer工具,这个可是大名鼎鼎的Oracle DBA最常使用的工具. 在单个文件不大的情况下(少于100000行),并且目的 ...
- spring结合mybatis实现数据库读写分离
随着系统用户访问量的不断增加,数据库的频繁访问将成为我们系统的一大瓶颈之一.由于项目前期用户量不大,我们实现单一的数据库就能完成.但是后期单一的数据库根本无法支撑庞大的项目去访问数据库,那么如何解决这 ...
- 配置Oracle访问SQL地理数据库
Oracle访问空间数据 ArcSDE是ArcGIS的空间数据引擎,它是在关系数据库管理系统(RDBMS)中存储和管理多用户空间数据库的通路.以前连接方式有两种,服务连接与直接连接(简称"直 ...