当前位置:网站首页>leetcode 39. 组合总和(完全背包问题)
leetcode 39. 组合总和(完全背包问题)
2022-08-09 21:54:00 【_刘小雨】
作者简介:C/C++ 、Golang 领域耕耘者,创作者
个人主页:作者主页
活动地址:CSDN21天学习挑战赛
题目来源: leetcode官网
如果感觉博主的文章还不错的话,还请关注 、点赞 、收藏🧡三连支持一下博主哦~~~
题目描述
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
🧡 算法分析
此题方法是用暴搜
算法步骤:
- 从原数组第0个元素开始,直接枚举到最后一个数字
- 枚举每次的时候需要满足
c[u] * i <= target
一个条件即可,即多个组合数之和小于等于目标值
代码实现
class Solution {
public:
vector<vector<int>> re;
vector<int> path;
vector<vector<int>> combinationSum(vector<int>& c, int target) {
// 暴搜即可, 从第0个元素开始
dfs(c, 0, target);
return re;
}
void dfs(vector<int>& c, int u, int target)
{
// 减到0 了,说明凑出来了其中之一结果
if(target == 0)
{
re.push_back(path);
return;
}
// 枚举到最后一个数,还没凑出来,直接return
if(u == c.size()) return ;
// 可以选择多少个
for(int i = 0 ; c[u] * i <= target; i ++)
{
dfs(c, u + 1, target - c[u] * i);
path.push_back(c[u]);
}
// 需要恢复现场
for(int i = 0; c[u] * i <= target; i ++)
{
path.pop_back();
}
return;
}
};
执行结果:
时间复杂度分析
暴搜,时间复杂度质数级别
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- 2.1.5 大纲显示问题
- CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
- 台风生成,广州公交站场积极开展台风防御安全隐患排查
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- Use convert_to_tensor in Tensorflow to specify the type of data
- 【GORM】模型关系-HasMany关系
- Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
- Use zeros(), ones(), fill() methods to generate data in TF
- 5个 Istio 访问外部服务流量控制最常用的例子,你知道几个?
- 电脑系统重装后怎么用打印机扫描出文件?
猜你喜欢
Simple questions peek into mathematics
从产品角度看 L2 应用:为什么说这是一个游乐场?
Flask入门学习教程
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
APP自动化测试框架-UiAutomator2基础入门
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
Bean life cycle
Flask之路由(app.route)详解
Under the NVM node installation;The node environment variable configuration
你真的了解乐观锁和悲观锁吗?
随机推荐
JS Deobfuscation - AST Restoration Case
Metasploit常用命令、技术功能模块
Basic JSON usage
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
Several ways to draw timeline diagrams
laravel table migration error [easy to understand]
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Let's talk about what DDL, DML, DQL and DCL are in SQL statements
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
Use zeros(), ones(), fill() methods to generate data in TF
random.normal() and random.truncated_normal() in TF
Flask入门学习教程
The round functions in the np, ceil function and floor function
从产品角度看 L2 应用:为什么说这是一个游乐场?
AI识万物:从0搭建和部署手语识别系统
Easyui 表单验证「建议收藏」
重装系统后新建文本文档打不开怎么办
Sudoku | Backtrack-7