29.Combination Sum(和为sum的组合)
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的组合)的更多相关文章
- c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...
- UVALive8518 Sum of xor sum
题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...
- dfs --path sum 问题 本质上就是组合问题(有去重)
135. 数字组合 中文 English 给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合. 在同一个组合中, ...
- Python神坑:sum和numpy.sum
同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...
- 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), ...
- two sum, three sum和four sum问题
1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash ...
- matalb sum函数和sum变量的用法
在对矩阵或者向量求和要用到sum函数的时候代码里面千万不要出现将sum作为变量名
- [Swift]LeetCode40. 组合总和 II | Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
随机推荐
- 如何显示当前Mipmap级别?
[如何显示当前Mipmap级别?] 乘以 mainTextureSize/mipTextureSize是为了让mipColorsTexture纹理与mainTexture级别对应.直接用uv是不行的, ...
- Jenkins中Jelly邮件模板的配置
[链接]Jenkins中Jelly邮件模板的配置http://blog.csdn.net/hwhua1986/article/details/47975237
- java-tip-HashMap
HashMap的基本查找过程: 先使用key.hashCode()生成哈希值,根据哈希值来确定key存放的位置 找到key在数组中的位置后,再使用key.equals()方法来找到指定的key. 1. ...
- 集合_java集合框架
转载自http://blog.csdn.net/zsw101259/article/details/7570033 Java集合框架图 简化图: Java平台提供了一个全新的集合框架.“集合框架”主要 ...
- OpenCV学习记录(二):自己训练haar特征的adaboost分类器进行人脸识别 标签: 脸部识别opencv 2017-07-03 21:38 26人阅读
上一篇文章中介绍了如何使用OpenCV自带的haar分类器进行人脸识别(点我打开). 这次我试着自己去训练一个haar分类器,前后花了两天,最后总算是训练完了.不过效果并不是特别理想,由于我是在自己的 ...
- [C++] struct memory allocation
MAX-byte alignment (最大单位对齐) typedef struct user USER; typedef struct employee E; struct user{ ]; //t ...
- 关于HBase的memstoreFlushSize。
memstoreFlushSize是什么呢? memstoreFlushSize为HRegion上设定的一个阈值,当MemStore的大小超过这个阈值时,将会发起flush请求. 它的计算首先是由Ta ...
- mysql:查询数据库版本的几种方式
Mysql版本: 登入数据库的时候: select @@version; select version(); mysql> select @@version; +-----------+ | @ ...
- Perl 学习笔记-输入输出
1.读取标准输入<STDIN>(行输入操作=> 读取一行直到换行符) chomp($line = <STDIN>); # 读取一行并去掉最后的换行符(不会自动去掉) pr ...
- 一起做RGB-D SLAM (3)
第三讲 特征提取与配准 2016.11 更新 把原文的SIFT替换成了ORB,这样你可以在没有nonfree模块下使用本程序了. OpenCV可以使用 apt-get install libopenc ...