[Leetcode 39]组合数的和Combination Sum
【题目】
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
- All numbers (including
Example 1:
Input: candidates =[2,3,6,7],target =7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5],target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
【思路】
回溯,不同在可以重复使用当前元素。相关题目
1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html
2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html
3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III
4、[Leetcode 39]组合数的和Combination Sum
【代码】
通俗版,重点在flag=i。
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
fun(ans,tmp,candidates,0,target);
return ans;
}
public void fun(List<List<Integer>> ans,List<Integer> tmp,int[] data,int flag,int aim){
if(aim<0)return;
else if(aim==0)
ans.add(new ArrayList<>(tmp));
else{
for(int i=flag;i<data.length;i++){
tmp.add(data[i]);
fun(ans,tmp,data,i,aim-data[i]);
tmp.remove(tmp.size()-1);
}
}
}
}
改进,ans设为全局变量,两个判断合并成一次
class Solution {
private static List<List<Integer>> res ;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
res = new ArrayList<>();
helper(candidates , 0 , target , new ArrayList<>());
return res;
}
private void helper(int[] input , int index , int target, List<Integer> temp) {
if (target<= 0) {
if (target == 0) {
res.add(new ArrayList<>(temp));
}
return ;
}
for (int i = index ; i < input.length ; i++) {
temp.add(input[i]);
helper(input , i , target - input[i] , temp);
temp.remove(temp.size() - 1);
}
}
}
[Leetcode 39]组合数的和Combination Sum的更多相关文章
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- Leetcode之回溯法专题-39. 组合总数(Combination Sum)
Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 【Leetcode】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 【Leetcode】【Medium】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【leetcode刷题笔记】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode每天一题】Combination Sum(组合和)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
随机推荐
- loj 6008 餐巾计划 - 费用流
题目传送门 传送门 题目大意 (经典题还不知道题意?) 容易想到需要把未使用的餐巾和已经使用的餐巾分开. 设$X_i$表示第$i$天已经的使用餐巾的点,设$Y_i$表示第$i$天还未使用的餐巾的点 我 ...
- Bootstrap常用单词组
布局容器 .container 固定宽度 .container-fluid 全屏 .row 行 .col-lg- 大屏幕 .col-md- 中屏幕 变量 @grid-columns: 12 列数 @g ...
- SPOJ 375 QTREE - Query on a tree
思路 注意本题只能用C,不能用C++ 其他的都和上一题一样 代码 #include <stdio.h> #include <string.h> #define MAXN 100 ...
- 3、Docker镜像管理基础
Docker image # docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE redis -alpine 23d561d12e92 d ...
- python自动化测试常用断言的使用方法
自动化测试中寻找元素并进行操作,如果在元素好找的情况下,大家就可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 下面介绍几个常用断言的使用方法,可以一定程度上帮助大 ...
- 利用GSEA对基因表达数据做富集分析
image Gene Set Enrichment Analysis (GSEA) is a computational method that determines whether an a p ...
- 大牛的IT经验,方法【跟,帮】
学习方法一:实践,应用,坚持. [swoole-韩天峰] 我最开始工作也是在2家小公司,后来加入腾讯阿里,主要原因还是我坚持学习基础知识,从而得到了这个机会.有几个方面的基础知识,我建议每一位PHP程 ...
- NetSec2019 20165327 Exp4 恶意代码分析
NetSec2019 20165327 Exp4 恶意代码分析 一.实践目标 1.监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分 ...
- ERROR:scala:Error:Object scala.runtime in compiler mirror not found
我的项目是sbt项目,今天早晨的时候还好好的,结果中午吃了个饭回来就特么的编译就报错了,真是闹心 报错截图: 解决方案: 删除idea的缓存文件 然后重新启动idea,重新下载jia包,在进行编译,就 ...
- myeclipse连接mysql生成数据表时中文字符乱码或问号(解决方法)
出现这个问题有以下三步解决思路: 1. 检查myeclipse的编码格式 windows---->Preferences---->general---->Workspace,右侧窗口 ...