Level:

  Medium

题目描述:

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]
]

思路分析:

​ 先对数组进行排序,方便后面递归回溯的过程中进行剪枝。然后设置一个变量sum,记录当前序列的数字和,如果sum的值等于target那么当前序列就是结果的一种,我们利用回溯的思想,找出所有满足要求得解。

代码:

public class Solution{
List<List<Integer>>res=new ArrayList<>();
public List<List<Integer>>combinationSum(int []candidates,int target){
if(candidates==null||candidates.length==0)
return res;
Arrays.sort(candidates);//排序,方便后面剪枝
find(candidates,0,0,target,new ArrayList<>());
return res;
}
public void find(int[]candidates,int start,int sum,int target,ArrayList<Integer>list){
if(sum==target){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i<candidates.length;i++){
if(sum+candidates[i]<=target){
list.add(candidates[i]);
find(candidates,i,sum+candidates[i],target,list);
list.remove(Integer.valueOf(candidates[i]));
}else{
break; //剪枝
}
}
}
}

29.Combination Sum(和为sum的组合)的更多相关文章

  1. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  2. UVALive8518 Sum of xor sum

    题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...

  3. dfs --path sum 问题 本质上就是组合问题(有去重)

    135. 数字组合 中文 English 给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合. 在同一个组合中, ...

  4. Python神坑:sum和numpy.sum

    同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...

  5. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  6. two sum, three sum和four sum问题

    1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash ...

  7. matalb sum函数和sum变量的用法

    在对矩阵或者向量求和要用到sum函数的时候代码里面千万不要出现将sum作为变量名

  8. [Swift]LeetCode40. 组合总和 II | Combination Sum II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

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

随机推荐

  1. 在springBoot在控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: logging.level.com.dy.springboot.server.mapper= ...

  2. C# 调用动态代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. node模块示例

    来源于慕课网课程:http://www.imooc.com/video/6701 (视频) 模块的流程图如下: 做一个学校的模块示例 建一个学生的js studet.js function add(s ...

  4. manta api

    Authentication 有几个访问方法. 验证对服务的请求的主要方法是使用TLS上的HTTP签名. 在大多数情况下,您只需使用SSH私钥对HTTP Date标头的小写日期:和值进行签名; 这样做 ...

  5. AM使用指南:如何在Managed Bean中获取AM实例?

    AM是放置服务方法的地方,有时我们需要在Managed Bean中调用这些方法.要调用这些方法,首先要在Managed Bean中获取AM实例.这里要用到<ADF工具类:ADFUtil.java ...

  6. rsyslog收集nginx日志配置

    rsyslog日志收集配置 rsyslog服务器收集各服务器的日志,并汇总,再由logstash处理 请查看上一篇文章  http://bbotte.blog.51cto.com/6205307/16 ...

  7. Zookeeper使用--Java API

    1  创建节点 创建节点有异步和同步两种方式.无论是异步或者同步,Zookeeper都不支持递归调用,即无法在父节点不存在的情况下创建一个子节点,如在/zk-ephemeral节点不存在的情况下创建/ ...

  8. Openssl rand命令

    一.简介 rand命令用来产生伪随机字节,随机数字产生器需要一个seed,在没有/dev/srandom系统下的解决方法是自己做一个~/.rnd文件 二.语法 openssl rand [-out f ...

  9. linux fuser的使用

    当进行共享存储的时候,umount可能无法用于卸载某个设备,说是被某个进程所占用,但是又无法找到该进程.这个时候使用fuser -km /data命令杀死所有在使用这个存储设备的进程然后再umount ...

  10. toFixed

    1,toFixed要使用与Number数据类型 2.parseInt将字符串转换为整形 3.parseFloat将字符串转换为浮点型