Leetcode 39.组合总数
组合总数
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
- 所有数字(包括 target)都是正整数。
- 解集不能包含重复的组合。
示例 1:
输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
解题思路:
我个人感觉该题目一点也不难,其实也是一个递归的过程,当达到递归条件时,就将结果加入结果集;
首先题目没说给的数组有啥特性,因此我先将数组进行了排序,这样在某个点找不着结果,那后面的都比target大,自然也就没有结果了。废话不多说,直接看代码;
代码如下:
import java.util.*;
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> LList = new ArrayList<List<Integer>>(); // 最终的结果集
if(candidates == null || candidates.length < 1 || target < 1 )
return LList;
Arrays.sort(candidates); // 排序,使得不用对相同的结果集计算多次
List<Integer> list = new ArrayList<Integer>(); // 临时结果保存
combinationSumCore(candidates,list, target, 0, LList); // 核心函数
return LList;
}
public void combinationSumCore(int[] candidates,List<Integer> list, int target, int index, List<List<Integer>> LList)
{
for(int i = index; i < candidates.length; i++)
{
if(candidates[i] == target) // 等于,就加入结果集
{
List<Integer> result = new ArrayList<Integer>();
result.addAll(list);
result.add(candidates[i]);
LList.add(result);
}
else if(candidates[i] < target) // 小于,就继续递归
{
List<Integer> result = new ArrayList<Integer>();
result.addAll(list);
result.add(candidates[i]);
combinationSumCore(candidates, result, target - candidates[i], i, LList); // 这边i值不变,是因为当前值可以使用多次
}
else // 大于,则后面的数字都大于,因此不可能出现在结果集中
{
break;
}
}
}
}
Leetcode 39.组合总数的更多相关文章
- Leetcode之回溯法专题-39. 组合总数(Combination Sum)
Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- Java实现 LeetCode 39 组合总和
39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- leetcode 39. 组合总和(python)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
随机推荐
- 删除".SVN"文件夹方法(转载)
转自:http://www.cnblogs.com/lr-ting/archive/2012/09/03/2666271.html 一.在linux下 删除这些目录是很简单的,命令如下 find . ...
- Git文件不显示图标/标识
初次使用Git服务功能,做了很多探路事情,记录下刚刚遇到的问题 情况:安装了Git应用程序,或者也安装了TortoiseGit-1.8.16.0-64bit(类似SVN工具)后,上传下载文件没有问题, ...
- npm 与 package.json 快速入门教程
npm 与 package.json 快速入门教程 2017年08月02日 19:16:20 阅读数:33887 npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解 ...
- vue实现全选,反选
1.example.vue <template> <table class="table-common"> <tr> <th class= ...
- [App Store Connect帮助]三、管理 App 和版本(2.6)输入 App 信息:新增 watchOS App 信息
如果您的 iOS App 中包含 watchOS App,请确保您的描述中包含该 App 在 Apple Watch 上的功能.您还需要为 Apple Watch 的 App Store 提供额外的屏 ...
- [Swift通天遁地]七、数据与安全-(4)CoreData数据的增、删、改、查
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- InterferenceSignal-----------挺简单的 一道题 就是英语不好
InterferenceSignal 时间限制: 2000ms内存限制: 128000KB 64位整型: Java 类名: 上一题 提交 运行结果 统计 讨论版 下一题 类型: 没有 添加 题目描述 ...
- 洛谷1002 容斥原理+dfs OR DP
//By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ,,,,-,-,-,- ...
- 从源码看ASP.NET框架(一)-打造页面控件树
测试实例如下: 前台代码MyFirstWeb.aspx(没有服务器控件,即没有runat) CodeBehind="MyFirstWeb.aspx.cs":表示代码后置类文件 In ...
- 商业计算中Java高精度计算BigDecimal类
<Effective Java> 第48条:如果需要精确的答案,请避免使用float和double. 如果我们编译运行下面这个程序会看到什么?public class Test{ p ...