LeetCode笔记:39. Combination Sum
题目描述


给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中。其中相同数的不同顺序组合算做同一种组合,candidates 中的数可以重复使用。
算法一
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);
}
}
}
算法二
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;
}
}
算法三
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的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- LeetCode OJ 39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
随机推荐
- LeetCode第二十一题-对两个有序链表排序
Merge Two Sorted Lists 问题简介:合并两个已排序的链表并将其作为新链表返回 举例: 输入: 1->3->5, 1->2->4 输出: 1->1-&g ...
- linux常用技能
阿里云镜像图形界面克隆虚拟机 linux替换阿里云镜像 centos6.6安装图形界面 克隆虚拟机后网络问题 linux替换阿里云镜像 第一步:备份你的原镜像文件,以免出错后可以恢复. cp /etc ...
- 医学图像数据(二)——TCIA完整数据集下载方式
1. 构建下载环境 l TCIA数据集下载文件为.jnlp格式(JNLP(Java Network Launching Protocol )是java提供的一种可以通过浏览器直接执行java应用程序 ...
- numpy数组取每一列的数据
也可以运用到列表中,a原本是一个列表的嵌套,将a转为了数组进行此操作,可以取固定的值,这就是numpy的好处.
- JSONP以及端口
跨域的方式有多种今天我呢,给大家带来的是JSONP接口的操作和接口 JSONP的接口到处都有 今天先拿BOOS直聘的来给大家演示一遍吧 首先找到boss官网:https://www.zhipin.co ...
- Date——js 获取当前日期到之后一个月30天的日期区间
var dateList = []; let startDate = new Date(); let endDate = new Date(); endDate.setDate(startDate.g ...
- 【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- Linux库多重依赖
源文件: //world.cpp #include <stdio.h> void world(void) { printf("world.\n"); } //hello ...
- Sprite子节点透明度不能跟随父节点变化的问题求解(转)
原出处忘记了. [已解决]Sprite子节点透明度不能跟随父节点变化的问题求解 自己封装了一个按钮控件,点击的时候封装了一些动作,其中有透明度的变化. 当点击发生的时候,Sprite本体执行正常,但是 ...
- 关于Java中static关键字的用法以及从内存角度解析
在Java中,static可以用来修饰成员变量和成员方法. 修饰成员变量,称为静态成员方法 修饰静态方法,称为静态成员方法 搞清楚用法和区别之前,先搞清static声明的变量和普通非静态变量在内存的分 ...