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 ...
随机推荐
- Javascript —— 有向图广度优先搜索
用Javascript实现有向图的广度优先搜索 刚好遇到一个需求,对于一个有向图,指定一个节点 i 作为起点,输出从 i 出发,可以到达的所有节点,也就是图中以 i 作为起点的子连通片,思考了一下,可 ...
- objective-C: NSString应该用initWithFormat? 还是 stringWithFormat?
今天在看书上的一段代码时,发现NSString实例化时,有时用的是initWithFormat方法,有时用的是stringWithFormat,到底应该如何选择呢? 区别: 1.initWithFor ...
- unity与android交互总结
http://www.jianshu.com/p/4739ce2f4cd1 http://www.cnblogs.com/suoluo/p/5443889.html http://www.th7.cn ...
- CloudStack 安装时需要的第三方包
1.mysql-connector-java 2.jakarta-commons-daemon-jsvc 3.jsvc rpm -ivh cloudstack-common-4.1.1-0.el6 ...
- spring4-3-AOP-AspectJ注解-01-简单使用
1.引入类库 <dependency> <groupId>org.springframework</groupId> <artifactId>sprin ...
- [Groovy]获取当前活动的Environment,获取response中节点的name和节点的value
import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...
- JDK1.6 1.7 1.8 多版本windows安装 执行命令java -version 版本不变的问题
现在Windows的java安装已经没有解压版本,Oracle官方也不会再提供了,只有安装程序 所以每当安装一次JDK,都会将 java.exe.javaw.exe.javaws.exe三个可执行文件 ...
- zigbee初探
什么是zigbee? 1.它是一种通信方式,一种通信协议: 2.其作用就是构建一个类似无线局域网的东西:如果这个局域网用于传感器的数据收集.监控,那么这个网络就叫做无线传感器网络. 应用领域:家居.工 ...
- Spring Boot☞HelloWorld开篇
目录结构 POM.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- VMware联网问题
VMware 服务启动优化当虚拟机连不上网,或不显示ip时,先查看vm服务是否启动.控制台运行:services.msc 以下为改为手工启动:1.VMware 服务自动启动影响计算机启动速度,再此对V ...