Combination Sum 组合数求和-Leetcode
时间:2022-05-04
本文章向大家介绍Combination Sum 组合数求和-Leetcode,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
原题:
Given a set of candidate numbers (C) 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.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- 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]
题意是找到所有加起来和为target值的组合,候选数可以重复使用。这明显是一道利用回溯的题目,可以想象成深度搜索。
以题目给出的【2,3,6,7】为例。一棵树根节点有四个子节点(2,3,6,7)。每个子节点下又有不小于本子节点的所有子节点。比如子节点2下面仍然有(2,3,6,7)四个子节点,子节点3下面有(3,6,7)三个子节点。我们就在这样一棵树上进行深搜,返回的条件是经过的节点的和为target。停止的条件是返回到根节点且无下一个子节点可用。
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
solve(candidates,0,target,new LinkedList<Integer>());
return ans;
}
private List<List<Integer>> ans=new LinkedList<>();
private void solve(int[] candidates,int start,int target,LinkedList<Integer> current){
if (target==0){
ans.add(copy(current));
return;
}
if (target<candidates[start]){
return;
}
else {
for (int iterator=start;iterator<candidates.length;iterator++){
if (candidates[iterator]>target){
break;
}
current.add(candidates[iterator]);
solve(candidates, iterator, target - candidates[iterator], current);
current.pollLast();
}
return;
}
}
private LinkedList<Integer> copy(LinkedList<Integer> source){
LinkedList<Integer> dest=new LinkedList<>();
for (int i:source){
dest.add(i);
}
return dest;
}
}
另一个效率更高的算法,逆向思维。
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
return combinationSum(candidates, target, 0);
}
public List<List<Integer>> combinationSum(int[] candidates, int target, int start) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
for (int i = start; i < candidates.length; i++) {
if (candidates[i] <target) {
for (List<Integer> ar : combinationSum(candidates, target - candidates[i], i)) {
ar.add(0, candidates[i]);
res.add(ar);
}
} else if (candidates[i] == target) {
List<Integer> lst = new ArrayList<>();
lst.add(candidates[i]);
res.add(lst);
} else
break;
}
return res;
}
}
- 2017.9.17校内noip模拟赛解题报告
- MySQL基础入门——MySQL与R语言、Python交互
- BizTalk Orchestration execute Flat file disassembler ReceivePipeline
- MySQL基础入门系列之——字符与日期数据处理
- P2038 无线网络发射器选址
- ggplot2双坐标轴的解决方案
- Modifying namespace in XML document programmatically
- ggplot2学习笔记——图例系统及其调整函数
- 一款脑洞大开的表格可视化神器
- P3908 异或之和
- P1939 【模板】矩阵加速(数列)
- R语言学习笔记之——数据处理神器data.table
- P3389 【模板】高斯消元法
- hihoCoder #1195 : 高斯消元·一
- 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 数组属性和方法
- NIO从入门到踹门
- GitHub CLI 1.0.0 正式发布!
- 【Flink】从零搭建实时数据分析系统
- Android模拟面试,解锁大厂——从Activity创建到View呈现中间发生了什么?
- 问你一个简单的问题:IP是怎么来的,又是怎么没的?
- 进击的NIO!Reactor模式!
- 交换机与VLAN:以办公室为例趣讲一波!
- 超详细Netty入门,看这篇就够了!
- Netty进阶之粘包和拆包问题
- 用侦察兵的故事趣讲ICMP和Ping,看完想忘都难!
- Kubernetes 使用 ceph-csi 消费 RBD 作为持久化存储
- ZooKeeper入门,看这篇就够了
- 超详细的RabbitMQ入门
- 用向量做Mantel的几个问题
- Eclipse集成Maven打包时报错:[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase