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 ...
随机推荐
- Maven 默认插件以及功能
Maven 默认插件 已知 Maven 使用 plugin 来执行实际操作的,在默认情况下,Maven 会绑定以下几个插件来完成基本操作. plugin function life cycle pha ...
- Android -- getWidth()与getMeasuredWidth()
getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...
- shell脚本用crontab执行和手动执行结果不一致
加上 PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin 这行就好了, shell首部用 #!/usr/bin/env bash 这个移植性更 ...
- Altium Designer 基本封装
1. 按键的绘制和封装怎么画?如下图: 回答:注意元件的画法不要看错了,封装采用Miscellaneous Devices.IntLib[Footprint View]中的DPST-4 2.蜂鸣器的绘 ...
- 网站运维之JAVA-SSH框架数据同步问题
一.环境 SSH环境,查询用的是基于Hibernate的配置文件构建了一个SessionFactory,主要代码如下 public class HibernateUtil { private stat ...
- Oracle整形转字符串to_char()
使用to_char()将NUMBER转换为字符串: select to_char(AW_PROCESSSTATUS ) as PROCESSSTATUS from A
- (算法)Travel Information Center
题目: Aps Island has many cities. In the summer, many travellers will come to the island and attend fe ...
- KVM 虚拟化技术
1.1 前言 1.1.1 什么是虚拟化? 在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU.内存.磁盘空间.网络适配器 ...
- 【转】开发者可以使用Docker做什么?
有些开发者可能还是不明白 Docker 对自己到底有多大的用处,因此翻译 Docker 个人用例 这篇文章中来介绍 Docker 在普通开发者开发过程中的用例. Docker 如今赢得了许多关注,很多 ...
- javascript中IE浏览器不支持NEW DATE()带参数的解决方法
代码如下: var date1=new Date(dateTimes[z][1]); 在火狐下 可以正常取得时间,在IE7下 却是 NaN.纠结老长时间,放弃了new date 然后再老外的论坛中找了 ...