给定数组,数组中的元素均为正数,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. 错误 未能找到类型或命名空间名称"xxxxxx"的真正原因

    今天又被这问题撞上了,结果神奇般的解决了 谷歌了很久都没有找到真正有用的解决方案,所以在这儿写下,让更多的人看到 最根本的原因其实就是引用的问题,引用错了,然后VS在这上面提示又不够智能,所以大家被坑 ...

  2. Redis系列-php怎么通过redis扩展使用redis

    From: http://blog.csdn.net/love__coder/article/details/8691679 通过前面几篇blog,我们应该对redis有个大致的认识,这里再讲解下,p ...

  3. linux中service的问题

    1.描述问题 2.解决方案 systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services ...

  4. Unity3D Shader基础教程

    原文地址:http://bbs.9ria.com/thread-212557-1-1.html 此教程将指引你如何建立自己的Shaders,让你的游戏场景看起来更好.Unity配备了强大的阴影和材料的 ...

  5. Java泛型概述

    泛型是Java中一个非常重要的知识点,在Java集合类框架中泛型被广泛应用.本文我们将从零开始来看一下Java泛型的设计,将会涉及到通配符处理,以及让人苦恼的类型擦除. 泛型基础 泛型类 我们首先定义 ...

  6. winform 实现选择文件和选择文件夹对话框

    //选择文件,点击[浏览],选择文件 private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileD ...

  7. level1 - unit 1 - 句子结构

    preface 学习英语做为一种爱好,就是希望有一天能够和老外流畅沟通,目前来看,日常沟通还是没有问题的和我的外教. 知识日积月累,现在就把以前学习的(从初中到现在)知识总结下.每天一更,更到单元总结 ...

  8. php命令

    今天因为psi无法在5.3版本上运行,正好看了下这些php版本的特性. 无意中,5.4版本 Buid-in web server内置了一个简单的Web服务器 $ php -S localhost: 于 ...

  9. chrome二维码插件 – w3cways QR Code Generator

    最近研究了下Chrome插件的制作方法,制作了一个二维码插件. 安装方法 方法一:在Chrome应用商店中安装(推荐)点击安装(需FQ) 方法二:本地安装:下载安装包,解压得到w3cways_qrCo ...

  10. Python_selenium二次封装selenium的几个方法

    Python_selenium二次封装selenium的几个方法 将常用的几个webdriver方法封装到自己写的一个类中去,此实例中是将"浏览器后退.浏览器前进.打开站点和关闭浏览器&qu ...