给定数组,数组中的元素均为正数,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. shell 下执行mysql 命令

    From: http://blog.csdn.net/beginning1126/article/details/8590014 方案1 mysql -uuser -ppasswd -e 优点:语句简 ...

  2. linux 上安裝lnmp

    1.確保有一台服務器可以正常運行 2.熟練知道一些基本的命令 3.這裡我以lnmp集成環境為例 https://lnmp.org/install.html 4.安裝大約30分鐘左右 5.安裝完畢,訪問 ...

  3. 图像处理、计算机视觉与模式识别“SCI期刊和顶级会议”总结

    期刊: best (1) IEEE Transactions on Pattern Analysis and Machine Intelligence,IEEE模式分析与机器智能汇刊,简称PAMI,是 ...

  4. Fedora ssh服务,防火墙服务设置

    Fedora26 ssh设置 ssh:connection to host 127,0.0.1 port22: Connection refused 首先安装openssh-server     yu ...

  5. Thinkphp5笔记三:创建基类

    做项目之前,一定要先部署要自己的"基类",非常的重要.打个比方,要访问会员中心相关的控制器,这类控制器是不是都要有一个“登录限制”,才让访问会员相关的控制器? 一.创建三大基类 原 ...

  6. mac ssh中文乱码解决

    网上有如下解决法,至少我没有成功过: vim ~/.bash_profile export LC_ALL='zh_CN.utf8' 来源:http://www.liuhuadong.com/archi ...

  7. Genymotion模拟器无法开启的解决方法——Unable to start the virtual device,The virtual device got no IP address

    前言 最近重装了电脑的系统,由win7换成了win8.1.android开发环境也重新配置了一遍.其他的都还好,就是genymotion模拟器一直开启失败. 自己尝试了很多方法,比如卸载重装软件,重新 ...

  8. get calllog fail

    coolpad Coolpad 8122   Uri smsUri = CallLog.Calls.CONTENT_URI;     Cursor callLogCursor = cr.query(s ...

  9. VCard介绍

    91助手和豌豆荚用VCard来存储通讯录,今天调查了一下. 1. 方案 使用VCard存储通讯录,文件扩展名为 vcf,  数据文件可以直接导入IPhone/Windows Phone/android ...

  10. smartgit 需要输入序列号解决办法

    找到路径: %APPDATA%\syntevo\SmartGit\<main-smartgit-version> 然后删除: settings.xml 再重新打开smartgit 就可以了