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, ...
随机推荐
- 2018-2019-2 网络对抗技术 20165325 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165325 Exp3 免杀原理与实践 实验内容(概要) 一.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己 ...
- #20175204 张湲祯 2018-2019-2《Java程序设计》第五周学习总结
20175204 张湲祯 2018-2019-2<Java程序设计>第五周学习总结 教材学习内容总结 -第六章接口与实现要点: -接口: 1.使用关键字interface定义接口. 2.接 ...
- Python——字符串
p ython——字符串 ①加法 连接两个字符串 ②乘法 复制字符串 python——转义字符 \n 换行 \' 单引号 \'' 双引号 \\ 反斜杠 raw字符串:无视转义字符 转义: 字符串 ...
- web文件下载
web页面实现文件下载的几种方法 今天碰到文件下载的一些问题,本着知其然也要知其所以然的精神,站在巨人的肩膀上深入学习和测试了一下,抛砖引玉,现在总结结论如下: 1)标准URL下载方式可以通过在web ...
- pads layout 自动打地孔
对于PCBLayout来说,后期处理也是一项费时操作,比如为了让板子耦合的更好,会在板子空旷位置打上很多地过孔.“自动打地孔”则会让你省时又省心,一不小心就提前了工期哦,哈哈! 一.下面打开一个简单的 ...
- OpenStack--ntp组件时间同步服务
作用:ntp主要是用于对计算机的时间同步管理操作 环境: 服务端: 192.168.245.172 客户端: 192.168.245.171 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器 ...
- [转]Go里面的unsafe包详解
Golang的unsafe包是一个很特殊的包. 为什么这样说呢? 本文将详细解释. 来自go语言官方文档的警告 unsafe包的文档是这么说的: 导入unsafe的软件包可能不可移植,并且不受Go 1 ...
- CocosCreator脚本中向依赖的组件赋值后, 被依赖的组件没有取到值的问题!
问题描述: 两个节点parent&child(其中都包含脚本组件), parent脚本组件依赖了child组件, 节点关系如下图: parent脚本内容如下: child脚本内容如下: 预览时 ...
- python3学习笔记十六
1. http协议 GET请求:数据放在url后面 POST请求:数据放在请求体中 <!DOCTYPE html> <html lang="en" ...
- ef core自动映射
原回答:https://stackoverflow.com/questions/26957519/ef-core-mapping-entitytypeconfiguration 一.反射 protec ...