当前位置:网站首页>Buckle practice - sum of 34 combinations
Buckle practice - sum of 34 combinations
2022-07-24 12:13:00 【qq_ forty-three million four hundred and three thousand six hun】
34 Combinatorial summation
1. Problem description
Given an array without repeating elements candidates And a target number target , find candidates All can make numbers and target The combination of , Number of output combinations .
candidates The number in can be selected repeatedly without limit .
explain :
All figures ( Include target) Positive integers .
A solution set cannot contain duplicate combinations .
Example 1:
Input :candidates = [2,3,6,7], target = 7,
The solution set is :
[
[7],
[2,2,3]
]
Output :2
Example 2:
Input :candidates = [2,3,5], target = 8,
The solution set is :
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Output :3
The following can be used: main function :
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. Enter description
First type candidates Length of array n,
Then input n It's an integer , Space off .
Last input target .
1 <= n <= 30
1 <= candidates[i] <= 100 candidates Each element in is unique .
1 <= target <= 100
3. The output shows that
Output an integer
4. Example
Input
3
2 3 5
8
Output
3
5. Code
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
//1. Search backtracking , For candidates Array , use idx Indicates the index of the currently traversed array , Array combine Used to represent the combined list ,ans Used to return the final result , At present there are target Need combination
void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans, vector<int>& combine, int index) {
//1. Termination conditions 1: After accessing all arrays
if (index == candidates.size()) {
return;
}
//2. Need to be combined target Have been to 0
if (target == 0) {
ans.push_back(combine);
//ans.emplace_back(combine);// List that has been combined combine Insert into ans
//push_back() Method to call the constructor and copy constructor , First construct a temporary object , And then put the temporary copy Constructor copies or moves to the back of the container .
//emplace_back() At the time of implementation , This element is created directly at the end of the container , It eliminates the process of copying or moving elements .
return;
}
// Directly skip the currently traversed element , Skip to the next element
dfs(candidates, target, ans, combine, index + 1);
// Select the currently traversed element
if (target - candidates[index] >= 0) {
combine.emplace_back(candidates[index]);
dfs(candidates, target - candidates[index], ans, combine, index);// Backtracking process
combine.pop_back();// Pay attention to the operation here
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;// Result array
vector<int> combine;// Lists that have been combined
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;
}
边栏推荐
- Anaconda environment migration
- Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
- 三层交换机配置MSTP协议详解【华为eNSP实验】
- Basic usage of GCC
- leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
- Leetcode:51. queen n
- Source code analysis sentry user behavior record implementation process
- [C and pointer Chapter 11] dynamic memory allocation
- 4*4图片权重的收敛规则
- 如何在IM系统中实现抢红包功能?
猜你喜欢

三层交换机配置MSTP协议详解【华为eNSP实验】

Equal principal increasing repayment / equal principal decreasing mortgage repayment calculator

Zhihuihuayun | cluster log dynamic collection scheme
![[data mining engineer - written examination] sheen company in 2022](/img/67/c61dfce8c397ab55fab835b6e81a5f.jpg)
[data mining engineer - written examination] sheen company in 2022

Leetcode:51. queen n
Learn some programming: anti unemployment "vaccine"
![[rust] Why do I suggest you learn rust | a preliminary study of rust](/img/33/a5e7d22e87502fa8582920cb34de9f.png)
[rust] Why do I suggest you learn rust | a preliminary study of rust

QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content

Summary of MySQL database combined with actual SQL optimization of the project

QT notes - qtxml
随机推荐
Online XML to CSV tool
Jackson parsing JSON detailed tutorial
如何将Typora中图片上传到csdn
makefile快速使用
容错、熔断的使用与扩展
Common formulas and application scenarios of discrete distribution
第0章 前言和环境配置
C Advanced - data storage
栈顶与栈底
OpenCV:08图像金字塔
Makefile quick use
Differences between JS map and foreach
C进阶——数据的存储
L2-011 play with binary tree
第1章 引言
SQL multi condition query cannot be implemented
Jmeter-While控制器
Common shortcuts to VIM editor
源码分析Sentry用户行为记录实现过程
VMware virtual machine and vSphere migrate to each other