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组合之和的更多相关文章

  1. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  4. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  6. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  7. LeetCode OJ:Combination Sum (组合之和)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  9. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

随机推荐

  1. 菜鸟Vue学习笔记(一)

    我今年刚参加工作,作为一个后台Java开发人员,公司让我开发前端,并且使用Vue框架,我边学习边记录. Vue框架是JS的封装框架,使用了MVVM模式,即model—view—viewmodel模式, ...

  2. rsync的daemon模式

    官方文档:https://download.samba.org/pub/rsync/rsyncd.conf.html   1:daemon模式配置文件         rsync以daemon方式运行 ...

  3. 1.2 VMware安装过程

    1.新建虚拟机后,选择[我以后再安装],否则安装过程看不到. 2.在虚拟机设置里,内存设置不能超过物理内存的一半. 3.创建硬盘 4.设置CD/ROM使用ISO镜像文件 5.创建[快照] 6.克隆虚拟 ...

  4. 【python】*与**

    1. 加了星号(*)的变量名会存放所有未命名的变量参数,不能存放dict,否则报错. 如: 1 def multiple(arg, *args): 2 print "arg: ", ...

  5. 依赖、耦合、解耦、控制反转(IOC)、依赖注入(DI)

    随着net的深入学习,出现了很多概念性的东西需要理解,现在统一记录一下. 1.依赖:现阶段在任何一个有请求作用的系统,都会出现A类调用B类的情况,这时候A类就依赖于B类,A类和B类存在依赖关系. 2. ...

  6. tinycc update VERSION to 0.9.27

    TinyCC全称为Tiny C Compiler, 是微型c编译器,可在linux/win/平台上编译使用. 在用代码里面使用tcc当脚本,性能比lua还快,目前已有网游服务端使用TCC脚本提高性能. ...

  7. 4.IIC总线

    一.IIC总线说明:      IIC总线时序只有高低电平的持续时间一般是大于多少us/ms.      iic时序:            开始:当SCL为高电平时,SDA由高电平状态切换到低电平状 ...

  8. springboot整合mybatis之注解方式

    1. 创建maven项目,工程目录如下图 2. 在pom.xml文件中添加mybatis依赖. 3. 创建实体类,并生成construct方法,getter和setter方法.同时在数据库中创建对应的 ...

  9. 【比赛打分展示双屏管理系统-加强版】的两个ini配置文件功能解释及排行榜滚动界面的简答配置等

    加强版目录下有两个ini文件,功能解释如下: 1. ScoreTip.ini: bScoreTip:如果为1,可以启用 回避 功能 或 高低分差值超出 iScoreRange 的 提示功能. iSco ...

  10. (整理)MySQL_REHL6.5 MySQL5.5 中文支持问题

    1 查看字符集命令 SHOW VARIABLES LIKE ‘character%’; 2 修改my.cnf文件中来实现中文支持 2.1 复制my.cnf 安装结束后,etc目录下并没有my.cnf文 ...