【LEETCODE】模拟面试-39. Combination Sum
和subset区别:规定了子集的sum==target
注意,这里传递的起始位置是i,而不是position+1,but why???
helper(res, path, candidate, sum - candidate[i], i);
code区别: notice:需要sort if条件里 sum >= candidate[i]
Arrays.sort(candidate); //需要sort
helper(res, path, candidate, sum, 0); //参数多了sum
//每次path加入新元素后,sum减去元素:sum - candidate[i]
if ( sum == 0 ) //stop条件是sum被减为0了
for ( int i = position; i < candidate.length && sum >= candidate[i]; i++ )
//sum余额不足覆盖下一个i的话,就不用再走了,这就是sort的好处
图:新生大学
https://leetcode.com/problems/combination-sum/
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set [2, 3, 6, 7] and target 7, A solution set is: [ [7], [2, 2, 3] ]
input: a candidate set, eg, [1, 2, 3, 6, 7]
, a target sum integer, eg, 4
output: find all possible unique combinations which can sum to be the target, and in each combination, the numbers can be repeated at unlimited times. eg, [2, 2] -> 4
We need a list of list
to store result, and a list
called path to store current single path. There is also a position
to locate the starting point for every path.
Notice the sum target
is able to decrease itself during the process.
For convenience, we will sort the candidate array
first.
The starting position
will iterate from 0 to candidate.length - 1
Since this question allows repeated numbers, for each path, we can do this repeated work
by passing the same starting point i
to next level, rather than i + 1
.
So that, for each unique number, the path will firstly add it for unlimited times until return. Then remove this number one by one, to leave space for next number. Moreover, the next number will also repeatedly add itself first until return.
And every time if current sum is larger than current number, we can add the number to current path, and pass the remaining sum = current sum - current number
to next level.
Until current sum is zero
, we will add the path to final result list.
After each path has finished its trip and returns to upper level, we should remove the last number of current path, to accept other new numbers.
public class Solution {
public List<List<Integer>> combinationSum(int[] candidate, int sum){
List<List<Integer>> res = new ArrayList<List<Integer>>();
//corner
//core
List<Integer> path = new ArrayList<>();
Arrays.sort(candidate);
helper(res, path, candidate, sum, 0); //start from position = 0
return res;
}
public void helper(List<List<Integer>> res, List<Integer> path, int[] candidate, int sum, int position){
//base
if ( sum == 0 ){
res.add(new ArrayList<Integer>(path));
return ;
}
//current
for ( int i = position; i < candidate.length && sum >= candidate[i]; i++ ){
path.add(candidate[i]);
//next: pass down remaining 'sum', and afterwards 'start position'
helper(res, path, candidate, sum - candidate[i], i);
path.remove(path.size() - 1);
}
return ;
}
}
- 如何掌握所有的编程语言
- 如何使用Sentry管理Hive外部表(补充)
- WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271)漏洞复现&修复方案
- 如何在CDSW中使用R绘制直方图
- CTF学习交流群 第一期入群题writeup大放送
- 如何使用Hue创建Spark1和Spark2的Oozie工作流
- 【译】深入研究 Laravel 的依赖注入容器
- 一次XSS突破的探险
- 如何使用Hue创建Spark2的Oozie工作流(补充)
- 如何基于CDSW基础镜像定制Docker
- OVSDB介绍及在OpenDaylight中的调用
- 如何在CDH集群的非元数据库节点安装MySQL5.7.12
- PySpark数据类型转换异常分析
- SQLI-LABS 更新帖(二)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Redis使用与操作k-v数据
- Spring集成Hadoop和Hbase
- JVM系列之:JIT中的Virtual Call接口
- 重新构建711的Android项目(二),架构的选择与实现
- Android的配置文件操作封装,摒弃SharedPreference操作配置漫天乱飞
- java实现PBOC的TLV格式解析,超简单的解析(全互联网最简单)
- 链表总计
- Spring整合MongoDb
- 正确使用Qt多线程
- Spring与hibernate与mybatis
- scala 模式匹配的几个模式
- Spring Security 认证
- Spring Security 简单配置用户存储
- 我的小工具-nodejs串口转TCP调试通信
- Spring Web Folw配置