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, ...
随机推荐
- 各种手工DIY
http://www.guokr.com/scientific/subject/diy/ 高仿宜家莲花灯 http://www.guokr.com/article/179863/ 做一只会发光的魔幻吊 ...
- ROS与C++
构建工作空间 catkin_make 构建Catkin包 catkin_create_pkg # This is an example, do not try to run this # catkin ...
- mysql-视图、触发器、事务、存储过程、流程控制
目录 视图 触发器 事务 存储过程 流程控制 一.视图 视图是由查询结果构成的一张虚拟表,和真实的表一样,带有名称的列和行数据 强调:视图是永久存储的,但是视图存储的不是数据,只是一条sql语句 视图 ...
- a标签锚点平滑跳转
一.创建锚点 <div class="header" id="top">//终点标签,添加一个id <a href="#top&qu ...
- 使用 DG Tweening
在iphone上卡顿的话,使用application.frame更改刷新帧率
- python3 集合(set)
一.定义:集合是一个无序不重复元素序列 语法: #---------------两种写法-------------------------# parame = {value1,value2,value ...
- trie字典树:初学
应用: 1.前缀问题 2.异或问题(转化为前缀问题) 3.查询问题 思想: 将要进行匹配的字符串化为一颗树 字符为边,在结束位置统计该串的全部信息 操作:插入,查询,删除.etc ac: #inclu ...
- Database Change Notification
使用java监控oracle数据库的变化,主要是针对表数据,如果发生变化,使用select去查询,能够达到推送的目的 package com.test.notifi; import java.sql. ...
- VIM懒人配置
VIM懒人配置 VIM配置起来,是很折腾人的.所以为了方便,直接使用前人的配置.重在用不在折腾. 1 VIM安装 一行命令. # sudo apt-get install vim 2 配置 vim的个 ...
- 小程序 公众号/h5相互跳转-webview
小程序与h5的跳转 前提小程序管理后台配置域名白名单,并且h5页面是嵌在小程序里面(相互跳的前提条件) 在业务域名中设置好访问的h5地址 微信官方web-view 介绍地址 https://devel ...