题目描述

给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中。其中相同数的不同顺序组合算做同一种组合,candidates 中的数可以重复使用。

算法一

首先想到的方法就是枚举所有的组合可能性,判断其和是否为target。枚举的方法可以使用递归,对candidates中每一个数,有“加入组合”和“不加入组合”两种选择,每一种选择又可以向后面元素的不同选择递归,直到candidate中最后一个元素。可以用剪枝来减少算法的运行时间,如果当前组合的和大于target,则当前情形下已不会有合适的组合了。
AC代码如下:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
dfs(candidates, 0, target, new ArrayList<Integer>(), res);
return res;
} private void dfs(int[] candidates, int index, int target, List<Integer> combination, List<List<Integer>> res) {
if(target < 0) return;
if(target == 0) {
res.add(new ArrayList<>(combination));
return;
} for(int i=index; i<candidates.length; i++) {
if(candidates[i] > target) continue;
//选择当前元素
combination.add(candidates[i]);
dfs(candidates, i, target - candidates[i], combination, res);
//不选择当前元素
combination.remove(combination.size() - 1);
}
}
}

算法二

本题符合动态规划的思想,用一个map记录不同target的全部组合,target <=  0的组合为空, target = i的组合为全部 target = i - candidates[j] (0<j<candidates.length)的组合加上candidates[j]。
这里需要注意,因为题目要求相同元素的不同顺序算同一种组合方式,上诉方法会出现[2,3,2] 和 [2,2,3]这样两种组合方式。可以用将每一种组合排序后加入set的方法来去重。
AC代码:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Map<Integer, List<List<Integer>>> dp = new HashMap<Integer, List<List<Integer>>>();
return dp(target, 0, candidates, dp);
} private List<List<Integer>> dp(int target, int index, int[] candidates, Map<Integer, List<List<Integer>>> map){
if(map.containsKey(target)) {
return map.get(target);
} List<List<Integer>> resList = new ArrayList<>();
Set<List<Integer>> resSet = new HashSet<>(); //这里一定要能够区分出 小于0和等于0. 等于0时加一个空的,避免出现[2,3,6,7] ,target = 7时,6被放入其中!!!!
if(target < 0 ) return resList;
if(target == 0){
resList.add(new ArrayList<>());
return resList;
} for(int i=index; i<candidates.length; i++){
List<List<Integer>> subResList = dp(target - candidates[i], index, candidates, map);
if(subResList.size() > 0) {
for(List<Integer> subRes : subResList) {
List<Integer> res = new ArrayList<>(subRes);
res.add(candidates[i]);
Collections.sort(res);
resSet.add(res);
}
}
}
for(List<Integer> l : resSet) {
resList.add(l);
} map.put(target, resList);
return resList;
}
}

算法三

本题还可以采用背包问题的思想,target相当于背包的容量,candidates为物品。
用一个map记录sum的范围从0~target的所有组合,容量为 i 的组合求解方式如下:遍历每一个物品candidates[j], 获取容量为 i - candidates[j]的所有组合,加入该物品。
这里值得注意的是,因为题目要求相同元素的不同顺序算同一种组合方式,因此需要将物品的循环放在容量循环的外面,这样就可以避免出现重复出现[2,3,2] 和 [2,2,3]这样两种组合方式。
AC代码:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Map<Integer, List<List<Integer>>> m = new HashMap<>();
m.put(0, new ArrayList<List<Integer>>()); for(int i=0; i<candidates.length; i++){
for(int j=0; j<=target; j++){
if(j < candidates[i]) continue;
List<List<Integer>> l = m.get(j - candidates[i]);
if(l != null) {
List<List<Integer>> jList = m.getOrDefault(j, new ArrayList<List<Integer>>());
if(l.size() == 0){
List<Integer> lcurr = new ArrayList<>();
lcurr.add(candidates[i]);
jList.add(lcurr);
}else{
for(List<Integer> listInL : l){
List<Integer> lcurr = new ArrayList<>(listInL);
lcurr.add(candidates[i]);
jList.add(lcurr);
}
}
m.put(j, jList);
}
}
}
return m.getOrDefault(target, new ArrayList<List<Integer>>());
}
}

LeetCode笔记:39. Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  3. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  4. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  5. LeetCode OJ 39. Combination Sum

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

  6. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  7. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  8. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  9. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  10. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

随机推荐

  1. flask学习(一)

    特点: 短小精悍,可扩展性强 依赖wsgi:werkzurg werkzurg示例: from werkzeug.wrappers import Request, Response from werk ...

  2. centos7.6设置sftp服务

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议. CentOS自带 SSH 服务,直接配置即可 1. 查看ssh版本 sftp是基于ssh协议的,首先查看 ...

  3. [Kubernetes]深入理解StatefulSet

    前面我写的一系列博客,如果你能够耐心看到这一篇,那你应该对一个概念就不是太陌生了:Deployment. 为什么提这个概念呢,这就要说到Deployment的一个不足了.Deployment不足以覆盖 ...

  4. 20175214 《Java程序设计》第8周学习总结

    20175214 <Java程序设计>第4周学习总结 前言:由于个人原因回家了两周,java学习进程落下了两周,且目前需交的实验报告较多,暂时无法补上前两次的博客,在将来会陆续补上,这次直 ...

  5. nginx+uwsgi+django开发环境搭建

    Nginx+uWSGI+Djangoi开发环境搭建 Django简介,环境搭建 uWSGI简介,安装与配置 Nginx安装与配置 Nginx+uWSGI+Django原理解析 1.django简介,环 ...

  6. Vue:window.onresize

    1. 添加属性screenHeight 和 timer. screenHeight: window.innerHeight timer: '' //  window.onresize函数频繁调用时,页 ...

  7. Mybatis面试集合(转)

    Mybatis技术内幕系列博客,从原理和源码角度,介绍了其内部实现细节,无论是写的好与不好,我确实是用心写了,由于并不是介绍如何使用Mybatis的文章,所以,一些参数使用细节略掉了,我们的目标是介绍 ...

  8. css背景图片充满DIV

    最近接手前端页面,让给调样式.哥纯粹一个代码程序猿,表示那些个样式应该让前端人员或者美工小妹妹来实现. 书归正传,碰到了问题,页面要在手机上展现,众所周知,手机在中国的牌子很多,很难做到统一. 页面上 ...

  9. 题解 P5315 【头像上传】

    本题就是按照题目模拟, 只是要注意一些细节问题. 看代码注释 #include<bits/stdc++.h> using namespace std; int n,l,g,i; int m ...

  10. css 自制一些小特效

    Github地址 位于gh-pages分支上 https://github.com/a1115040996/MyHTML/ 3D 卡片特效 地址: https://a1115040996.github ...