当前位置:网站首页>力扣练习——34 组合总和
力扣练习——34 组合总和
2022-07-22 07:48:00 【qq_43403657】
34 组合总和
1.问题描述
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合,输出组合的数量。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。
示例 1:
输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
输出:2
示例 2:
输入:candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
输出:3
可使用以下main函数:
int main()
{
int n,data,target;
vector<int> candidates;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>data;
candidates.push_back(data);
}
cin>>target;
vector<vector<int> > res=Solution().combinationSum(candidates,target);
cout<<res.size()<<endl;
return 0;
}
2.输入说明
首先输入candidates数组的长度n,
然后输入n个整数,以空格分隔。
最后输入target 。
1 <= n <= 30
1 <= candidates[i] <= 100 candidates 中的每个元素都是独一无二的。
1 <= target <= 100
3.输出说明
输出一个整数
4.范例
输入
3
2 3 5
8
输出
3
5.代码
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
//1.进行搜索回溯,其中对于candidates数组,用idx表示当前遍历的数组下标,数组combine用来表示已经组合的列表,ans用来返回最后的结果,目前还有target需要组合
void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans, vector<int>& combine, int index) {
//1.终止条件1:访问完所有数组
if (index == candidates.size()) {
return;
}
//2.需要进行组合的target已经为0
if (target == 0) {
ans.push_back(combine);
//ans.emplace_back(combine);//将已经组合的列表combine插入到ans
//push_back()方法要调用构造函数和复制构造函数,先构造一个临时对象,然后把临时的copy构造函数拷贝或者移动到容器最后面。
//emplace_back()在实现时,则是直接在容器的尾部创建这个元素,省去了拷贝或移动元素的过程。
return;
}
// 直接跳过当前被遍历的元素,跳到下一个元素
dfs(candidates, target, ans, combine, index + 1);
// 选择当前被遍历的元素
if (target - candidates[index] >= 0) {
combine.emplace_back(candidates[index]);
dfs(candidates, target - candidates[index], ans, combine, index);//回溯过程
combine.pop_back();//注意这里的操作
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;//结果数组
vector<int> combine;//已经组合的列表
dfs(candidates, target, ans, combine, 0);
return ans;
}
int main()
{
int n, data, target;
vector<int> candidates;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> data;
candidates.push_back(data);
}
cin >> target;
vector<vector<int> > res = combinationSum(candidates, target);
cout << res.size() << endl;
return 0;
}
边栏推荐
- JS to determine whether the linked image exists
- Can flick SQL query Clickhouse
- 线性回归(公式推导+numpy实现)
- Android interview question: what is the difference between pendingintent and intent?
- Bigder:37/100 a misoperation
- Using dichotomy to find the elements of an array
- MySQL约束
- How does boss direct hire write an excellent resume?
- subprocess
- MySQL系列文章之四:执行计划
猜你喜欢
Ajout, suppression et modification de MySQL (niveau avancé)
Bigder:37/100 一个误操作
Data Lake (18): Flink and iceberg integrate SQL API operations
MySQL 增删改查(進階)
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
【云原生】Docker部署数据库的持久化
2022-07-21: given a string STR and a positive number k, you can divide STR into multiple substrings at will. The purpose is to find that in a certain division scheme, there are as many palindrome subs
使用 Abp.Zero 搭建第三方登录模块(四):微信小程序开发
【数据库】MySQL表的增删改查(进阶)
【HMS core】【FAQ】【Account Kit】典型问题合集1
随机推荐
【Harmony OS】【ARK UI】【Demo】加载动画实现
Deep learning (II) takes you to understand neural networks and activation functions
How does Oracle set up not to check compilation errors during creation?
【数据库】MySQL表的增删改查(进阶)
Data link layer of network (PPP Protocol)
Bigder:38/100 一个误操作的问题解决了
异地多活的演变
Replace spaces in niuke.com
[HMS core] [push kit] set of message classification problems
MySQL约束
【HMS core】【FAQ】【Account Kit】典型问题合集1
[HMS core] [FAQ] [account kit] typical problem set 2
Excel导入导出Controller
Pytorch实现Word2Vec
【数据库】MySQL表的增删改查(基础)
【论文汇总】2D目标检测文章汇总,持续更新
How is VR panorama displayed in all walks of life? How to implement the application?
毕业985,工作996,也躲不开中年危机
Distributed (I) what is sacred about distributed systems, base and cap?
QWidget add right-click menu