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. mysql的my.ini配置文件

    第一步,我们找到mysql安装文件下面的my.ini文件,打开可以看到第一句: # MySQL Server Instance Configuration File Mysql服务实例配置文件 好,咱 ...

  2. PHPexcle案例

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...

  3. Unable to resolve module `../res/images/ic_popular.png`

  4. labelimg

    ------------------------labelimg------------------------- cd /home/luo/TensorflowProject/labelImg py ...

  5. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

  6. hive1.2.1问题集锦

    1.启动hive报错: Logging initialized using configuration in jar:file:/usr/local/hive-1.2.1/lib/hive-commo ...

  7. CDATA嵌套问题

    在CDATA内部的所有内容都会被解析器忽略.一个 CDATA 部件以"<![CDATA[" 标记开始,以"]]>"标记结束.但是CDATA是不能够嵌 ...

  8. 视觉SLAM漫淡(二):图优化理论与g2o的使用

    视觉SLAM漫谈(二):图优化理论与g2o的使用 1    前言以及回顾 各位朋友,自从上一篇<视觉SLAM漫谈>写成以来已经有一段时间了.我收到几位热心读者的邮件.有的希望我介绍一下当前 ...

  9. [GO]用go语言实现区块链工作原理

    基本原理这里就不写了,只写一个简单demo的实现 首先得有一个区块用来存储区块头和区块体 type Block struct { Version int64 PreBlockHash []byte H ...

  10. [GO]结构体指针变量初始化

    package main import "fmt" func main() { type student struct { id int name string sex byte ...