leetcode 39 : 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;/*** 题目:* 组合总和 -- leetcode 39* <p>* 题目描述:* <p>* 给定一个无重复元素的数组 candidates 和一个目标数 target ,* 找出 candidates 中所有可以使数字和为 target 的组合。* candidates 中的数字可以无限制重复被选取。* <p>* 说明:* 所有数字(包括 target)都是正整数。* 解集不能包含重复的组合。* <p>* 示例 1:* 输入: candidates = [2,3,6,7], target = 7,* 所求解集为:* [* [7],* [2,2,3]* ]* <p>* 示例 2:* 输入: candidates = [2,3,5], target = 8,* 所求解集为:* [* [2,2,2,2],* [2,3,3],* [3,5]* ]*/
public class CombinationSum {public static void main(String[] args) {{int[] candidates = new int[]{2, 3, 5};int target = 8;System.out.println(combinationSum(candidates, target));System.out.println(combinationSumII(candidates, target));}{int[] candidates = new int[]{2, 3, 6, 7};int target = 7;System.out.println(combinationSum(candidates, target));System.out.println(combinationSumII(candidates, target));}}/*** https://www.jianshu.com/p/42211be17acb* 思路:* 1、回溯算法* 2、递归找和为target的组合,出口为和超过了target*/public static List<List<Integer>> combinationSum(int[] arr, int target) {List<List<Integer>> res = new ArrayList<>();if (arr == null) {return res;}addCombinations(arr, 0, target, new ArrayList<>(), res);return res;}private static void addCombinations(int[] arr,int start,int target,List<Integer> cache,List<List<Integer>> res) {if (target < 0) {return;}if (target == 0) {res.add(new ArrayList<>(cache));return;}for (int i = start; i < arr.length; i++) {cache.add(arr[i]);addCombinations(arr, i, target - arr[i], cache, res);cache.remove(cache.size() - 1);}}/*** https://www.jianshu.com/p/42211be17acb* 思路:* 优化后的回溯*/public static List<List<Integer>> combinationSumII(int[] arr, int target) {List<List<Integer>> res = new ArrayList<>();if (arr == null) {return res;}// 排序数组后 可以在递归的时候减少递归次数,配合 if (arr[i] > target) break;Arrays.sort(arr);addCombinationsII(arr, 0, target, new ArrayList<>(), res);return res;}private static void addCombinationsII(int[] arr,int start,int target,List<Integer> cache,List<List<Integer>> res) {if (target < 0) {return;}if (target == 0) {res.add(new ArrayList<>(cache));return;}for (int i = start; i < arr.length; i++) {// 配合排序后的数组 提升性能if (arr[i] > target) {break;}cache.add(arr[i]);addCombinationsII(arr, i, target - arr[i], cache, res);cache.remove(cache.size() - 1);}}}
leetcode 39 : 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。相关推荐
- leetcode C++ 39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 c
一.思路: DFS深度搜索,直到所有元素都被遍历.另外如果一组结果的求和大于target,剪枝返回 class Solution { public:vector<vector<int> ...
- java数组出现次数最多的数_找出数组中出现次数最多的那个数——主元素问题...
方法一:以空间换时间,可以定义一个计数数组int count[101],用来对数组中数字出现的次数进行计数(只能针对数组中数字的范围1~100),count数组中最大的元素对应的下标,即为出现次数最多 ...
- [Python] np.unique(ndarray) 返回ndarray的无重复元素的数组
官方文档:numpy.unique NumPy v1.17 Manual 语法: np.unique(ndarray) 实例: gt = [[1,2,2],[3,3,4]] temp = np.uni ...
- 【LeetCode笔记】3. 无重复字符的最长子串(JAVA、滑动窗口、字符串)
文章目录 题目描述 思路 && 代码 1. 之前的版本 更新 2.0 题目描述 子串:各字符间必须要相邻,而非子序列 使用滑动窗口来做就行 思路 && 代码 1. 之前 ...
- LeetCode 217、219. 存在重复元素
217. 存在重复元素 题目:给你一个整数数组 nums .如果任一值在数组中出现 至少两次 ,返回 true :如果数组中每个元素互不相同,返回 false . 链接 https://leetcod ...
- Leetcode题库217.存在重复元素(python实现)
文章目录 思路 代码 思路 1.采用set的性质(无重复元素)解题 2.hash数组 3.字典 4.暴力for循环(小心超时) 代码 class Solution:def containsDuplic ...
- leetcode初级算法3.存在重复元素
leetcode初级算法3.存在重复元素 仅为个人刷题记录,不提供解题思路 题解与收获 我的解法: Arrays.sort(nums);for(int i = 0; i < nums.lengt ...
- leetcode-java.T015_3Sum---给定一个n个元素的数组,是否存在a,b,c三个元素,使用得a+b+c=0,找出所有符合这个条件的三元组
<span style="color:#ff4635">敬请关注博客,后期不断更新优质博文,谢谢</span> package leetcode.T015_ ...
- 每周算法题(从三个红球、五个白球、六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案,有1、2、3、4数字,组成无重复的三位数两个乒乓球队进行比赛,各出三人。甲队为a,b,c)
每周算法题 文章目录 每周算法题 一.从三个红球.五个白球.六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案. 二.有1.2.3.4数字,组成无重复的三位数 两个乒乓球队进行比赛,各出三 ...
- 数据结构第5章例题 若矩阵Am×n中存在某个元素aij满足:aij是第i行中的最小值且是第j列中的最大值,则称该元素为矩阵A的一个鞍点。试编写一个算法,找出A中的所有鞍点。
[例5.1] 若矩阵Am×n中存在某个元素aij满足:aij是第i行中的最小值且是第j列中的最大值,则称该元素为矩阵A的一个鞍点.试编写一个算法,找出A中的所有鞍点. 算法如下: void saddl ...
最新文章
- java保护型数据成员_Java基础知识笔记第四章:类和对象
- pymysq向mysql写数据 为什么本地无法查看_从运维角度浅谈MySQL数据库优化,中小企业DBA必会...
- 关于自动驾驶,我们是否在刻意回避这三大关键问题?
- day03-正则表达式
- 鼠标滚轮控制panel滚动条
- xshell突出显示集
- 「技美之路」图形 1.1 渲染流水线
- HDU 3974 Assign the task(dfs时间戳+线段树成段更新)
- java集合——具体的集合
- 动态表单,JSF世界早已等待
- mysql改date格式_mysql数据库修改添加Date格式列的方法
- 搭建Kafka集群环境
- (51)FPGA面试题-Verilog中function与task之间的区别是什么?
- 相聚“金巴蜀”、付诸笑谈中
- mac git 拉代码太慢或是拉不下来,可能是这个原因
- 17/18/19每日一练汇总
- 移动端日期插件rolldate
- chrome应用程序无法正常启动0x0000005
- xp计算机启动检测硬盘,取消WinXP开机自检技巧五则
- 「2017ACM/ICPC亚洲区域赛北京站现场赛G」Liaoning Ship's Voyage【计算几何】