【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 ;
}
}
- 【微信开发】 使用单例设计模式 提供AccessToken 和Jsapi_ticket缓存支持
- @ResponseBody响应JSON 406
- tomcat不能运行或共存多个项目
- Spring Task 定时任务
- 如何为机器学习索引,切片,调整 NumPy 数组
- 逆向工程分析:摩托罗拉安全摄像头究竟有多不安全?
- 【maven学习】 利用Profile构建不同环境的部署包
- 如何写好一份渗透测试报告?
- jqGrid 关闭刷新父页面数据
- [MyBatis]诡异的Invalid bound statement (not found)错误
- jfinal自定义freemarker标签
- Angrok 一个内网穿透服务
- Druid数据库连接池 实现数据库账号密码加密
- 【maven学习】划分模块
- 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 数组属性和方法
- java中BigDecimal使用和注意事项
- redis实战第十一篇 redis cluster添加密码
- Linux下docker制作springboot应用镜像
- 行为型设计模式:备忘录模式
- 行为型设计模式:访问者模式
- redis实战第十篇 集群收缩
- redis实战第九篇 集群扩容自动迁移槽(redis-cli)
- 自动化运维| Ansible playbook的逻辑控制语句
- Windows通用应用平台UWP持久化
- redis实战第八篇 集群扩容 手动迁移槽
- Xinetd服务的安装与配置详解
- redis实战第七篇 使用redis工具(redis-cli)搭建redis cluster
- 使用ROS2机器人操作系统进行多机器人编程技术实践(Multi-Robot Programming Via ROS2 )
- 自动化运维实践 | Ansible playbook重用
- 手把手教你打造高效的 Kubernetes 命令行终端