给定数组,数组中的元素均为正数,target也是正数。(数组中的元素可能有重复)
求出所有的满足求和等于terget的组合。
数组中的元素只能使用一次。(数组中重复的元素可以最多使用重复次数)
 
参考代码: 
package leetcode_50;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /***
*
* @author pengfei_zheng
* 数组中求和等于target的所有组合
*/
public class Solution40 {
public List<List<Integer>> combinationSum2(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, target, 0);
return list; } private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){
if(remain < 0) return;
else if(remain == 0) list.add(new ArrayList<>(tempList));
else{
for(int i = start; i < nums.length; i++){
if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
tempList.add(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
}

LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)的更多相关文章

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

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

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

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

  3. [LeetCode] 40. Combination Sum II 组合之和 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 组合之和之二

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

  6. leetcode 40 Combination Sum II --- java

    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 (组合的和之二)

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

  9. Java [Leetcode 40]Combination Sum II

    题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...

随机推荐

  1. ubuntu16.04卸载tensorflow0.11版本,安装tensorflow1.1.0版本

    卸载旧版本: pip uninstall tensorflow 安装新版本: sudo pip install --upgrade https://storage.googleapis.com/ten ...

  2. u3d外部资源加载加密

    原文地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为 ...

  3. 【.NET】正则表达式笔记

    很早就听说正则表达式的强大,今天终于一睹它的真容,在这里记下学习时候的笔记,以便以后查看 1.正则表达式 用于描述字符串规则的的特殊的字符(正则表达式本身是字符串,用来描述字符串的相关规则,用于与其他 ...

  4. SharePoint PowerShell部署开发好的WebPart到服务器上

    内容仅供参考,需结合实际需求来处理. =========SharePoint 环境下运行ps1文件,ps1内容如下======= Set-ExecutionPolicy ByPass Add-PSSn ...

  5. Java编程思想学习笔记——枚举类型

    前言 关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 正文 基本enum特性 调用enum的values()方法可以遍历enum实例,value ...

  6. 1 最简单的hello world

    preface 今天我开始自学flask了,由此记录学习中的点点滴滴. 有问题请联系我(Mr.Leo 18500777133@sina.cn) include: 简介flask hello world ...

  7. Windows 安装 adt-bundle的方法

    Refer:http://my.eoe.cn/shuhai/archive/19381.html Windows 安装 adt-bundle的方法 很多大神说Windows下Eclipse启动不起来, ...

  8. WebGL 颜色与纹理

    1.纹理坐标 纹理坐标是纹理图像上的坐标,通过纹理坐标可以在纹理图像上获取纹理颜色.WebGL系统中的纹理坐标系统是二维的,如图所示.为了将纹理坐标和广泛使用的x.y坐标区分开来,WebGL使用s和t ...

  9. log4j MDC用户操作日志追踪配置

    一.MDC介绍 MDC(Mapped Diagnostic Context,映射调试上下文)是 log4j 和 logback 提供的一种方便在多线程条件下记录日志的功能.某些应用程序采用多线程的方式 ...

  10. DotNetBar如何控制窗体样式

    在C#中使用控件DevComponents.DotNetBar时,如何创建一个漂亮的窗口,并控制窗体样式呢?   1.新建一个DotNetBar窗体             2.使OFFICE窗口风格 ...