题目描述:

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[] candidates,
int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
} public void getResult(List<List<Integer>> result,
List<Integer> current, int[] candiates, int target, int start) {
if (target > 0) {
for (int i = start; i < candiates.length && target >= candiates[i]; i++) {
if (i > start && candiates[i] == candiates[i - 1])
continue;
current.add(candiates[i]);
getResult(result, current, candiates, target - candiates[i],
i + 1);
current.remove(current.size() - 1);
}
} else if (target == 0) {
result.add(new ArrayList<Integer>(current));
}
}
}

  

Java [Leetcode 40]Combination Sum II的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. leetcode 40 Combination Sum II --- java

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. Leetcode 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

  9. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

随机推荐

  1. 拓展:return和print的使用时机

    拓展:return和print的使用时机  一直纠结函数里的return用法.以下内容摘自百度知道..def 是用来定义函数的一个关键字,只有在函数的定义时用到他.Python 函数定义的语法:def ...

  2. 1010. Radix (25)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  3. 2016年辛星less教程发布了

    首先简介一下less吧,它是一个css预处理器.当我们的项目比较大的时候,我们的css的代码量也会急剧的膨胀,因此就有很多方案来让编程更加容易,没错,less就是这样一种技术. 在百度网盘的下载地址为 ...

  4. Eclipse开发Android报错Jar mismatch! Fix your dependencies

    常常打开工程,发现项目并没有错,但是会有一个红X,然后就生成不了. 发现两个版本不同的android-support-v4.jar在使用 打开window-show views-problems Ja ...

  5. NGUI系列教程十(Scroll View实现触摸滚动相册效果)

    NGUI中提供了两种Scroll View 一种是通过手指或鼠标滑动视图时移动平面物体,另一种则是直接移动摄像机,他们各有各的好处.但是NGUI提供的Scroll View很难实现类似Android ...

  6. 关于mapreduce过程中出现的错误:Too many fetch-failures

    Reduce task启动后第一个阶段是shuffle,即向map端fetch数据.每次fetch都可能因为connect超时,read超时,checksum错误等原因而失败.Reduce task为 ...

  7. Jquery异步请求简单实例(转)

    本文引用自Xingsoft. 一.Jquery向aspx页面请求数据 前台页面JS代码:             $("#Button1").bind("click&qu ...

  8. 使用Yeoman搭建 AngularJS 应用 (6) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/review-generated-files.html 打开mytodo文件夹,你会看到现在的基架.如下图所示 在mytodo文件夹,我们能 ...

  9. EL表达式中如何截取字符串

    EL表达式中如何截取字符串 可以截取,用fn函数:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/ ...

  10. PHP5 session 详解

    http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...