当前位置:网站首页>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;
}
};
执行结果:

时间复杂度分析
暴搜,时间复杂度质数级别
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- [Microservice~Nacos] Configuration Center of Nacos
- 论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
- Sudoku | Backtrack-7
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- 蔚来杯2022牛客暑期多校训练营7 CFGJ
- 你真的了解乐观锁和悲观锁吗?
- JS解混淆-AST还原案例
- CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
- Multiple reasons for MySQL slow query
- TF中random.normal()与random.truncated_normal()
猜你喜欢

Activiti7审批流

深度剖析 Apache EventMesh 云原生分布式事件驱动架构
![[Cloud Native] 4.2 DevOps Lectures](/img/d0/2cd32351234401fdfecd3a829a639d.png)
[Cloud Native] 4.2 DevOps Lectures

为什么这么多人都想当产品经理?

CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source

万字总结:分布式系统的38个知识点

在“企业通讯录”的盲区,融云的边界与分寸

Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum

TF generates uniformly distributed tensor
![[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]](/img/49/ebedcd4d27aa608360ac17e504f36d.png)
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
随机推荐
台风生成,广州公交站场积极开展台风防御安全隐患排查
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
AI识万物:从0搭建和部署手语识别系统
电脑系统重装后怎么用打印机扫描出文件?
Js fifteen interview questions (with answers)
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
Under the NVM node installation;The node environment variable configuration
TF生成均匀分布的tensor
[Microservice~Nacos] Nacos service provider and service consumer
nvm下node安装;node环境变量配置
Ehrlich screening method: Counting the number of prime numbers
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
[Cloud Native] 4.2 DevOps Lectures
Flask introductory learning tutorial
MLOps的演进历程
laravel table migration error [easy to understand]
[Microservice~Nacos] Configuration Center of Nacos
STC8H development (15): GPIO drive Ci24R1 wireless module
Pagoda measurement - building LightPicture open source map bed system