[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.
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]
]
题意:
给定一个集合以及一个值target,找出所有加起来等于target的组合。(每个元素可以用无数次)
Solution1: Backtracking
code:
/*
Time: O(n!) factorial, n!=1×2×3×…×n
Space: O(n) coz n levels in stack for recrusion
*/ class Solution {
public List<List<Integer>> combinationSum(int[] nums, int target) {
Arrays.sort(nums); // 呼应dfs的剪枝动作
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
dfs(nums, path, result, target, 0);
return result;
} private static void dfs(int[] nums, List<Integer> path,
List<List<Integer>> result, int remain, int start) {
// base case
if (remain == 0) {
result.add(new ArrayList<Integer>(path));
return;
} for (int i = start; i < nums.length; i++) {
if (remain < nums[i]) return; //基于 Arrays.sort(nums);
path.add(nums[i]);
dfs(nums, path, result, remain - nums[i], i);
path.remove(path.size() - 1);
}
}
}
[leetcode]39. Combination Sum组合之和的更多相关文章
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 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 (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- 使用maven-tomcat7-plugins时调试出现source not found解决
直接看下面的步骤: 步骤1: 步骤2: 步骤3: 步骤4:
- java 各种循环遍历
遍历方式选择: 实现了 RandomAccess 接口的 list,优先选择普通 for 循环 ,其次 foreach: 未实现 RandomAccess 接口的 list, 优先选择 iterato ...
- istream不是std的成员
如果报错信息为:istream不是std的成员,那么有两种可能 1.没有包含iostream库文件 2.#ifndef 和#endif使用错误,致使包含的iostream的头文件没有被主函数包含
- buffers和cached的区别
原文:https://www.cnblogs.com/kevingrace/p/5991604.html buffers和cached解释 ============================== ...
- 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) ...ubuntu 18.04 安装vim遇到的错误
安装vim: sudo apt-get install vim-gtk 问题: E:无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E:无法锁定管理目录(/ ...
- redis学习链接收藏
1.redis命令大全--官网 2.redis命令大全--中文翻译版 3.源码(注释版):redis3.0 4.程序代码:<redis入门指南(第二版)>第五章 5.最新的redis-st ...
- 01.JDBC操作数据库-快速入门操作
/** * 简单入门操作 * 注:先将mysql-connector-java-5.1.36.jar 构建 Build Path环境当中去 * @param args * @throws Except ...
- 换上 SansForgetica-Regular 字体,增加记忆能力
最近澳大利亚的RMIT(皇家墨尔本理工大学) 搞出来这么个字体,号称能增强记忆,原理是通过难以识别的字体,让人提起精神去识别,从而记忆更深刻. 果断弄了个试试. 安装过程: 下载字体文件 点这里去下载 ...
- linux服务之apache(二)
1.ip/pv/uv(用来统计网站被访问情况) ip:表示该网站一天被多少ip访问过,一天一个ip之算做一次. pv:表示页面被访问的次数 uv:独立访客,一个用户就是一个uv. 2.创建虚拟主机 利 ...
- linux如何复制文件夹和移动文件夹
linux下文件的复制.移动与删除命令为:cp,mv,rm一.文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)cp [option ...