当前位置:网站首页>Buckle exercise - 35 combination sum II
Buckle exercise - 35 combination sum II
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
35 Combinatorial summation II
1. Problem description
Given an array candidates And a target number target , find candidates All can make numbers and target The combination of , Number of output combinations .
candidates Each number in can only be used once in each combination .
explain :
All figures ( Include target number ) Positive integers .
A solution set cannot contain duplicate combinations .
Example 1:
Input : candidates = [10,1,2,7,6,1,5], target = 8,
The solution set is :
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Output :4
Example 2:
Input : candidates = [2,5,2,1,2], target = 5,
The solution set is :
[
[1,2,2],
[5]
]
Output :2
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().combinationSum2(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 <= 60
1 <= candidates[i] <= 100 candidates There are duplicates of elements in .
1 <= target <= 100
3. The output shows that
Output an integer
4. Example
Input
5
2 5 2 1 2
5
Output
2
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) {
int back=0;
// Termination conditions 1: Need to be combined target Have been to 0
if (target == 0) {
ans.push_back(combine);
return;
}
// Termination conditions 2: After accessing all arrays // There may be cases where the last element after the sequence is a combination , Avoid omitting this situation
if (index == candidates.size()) {
// Be careful , The judgment condition here must be placed on target==0 Back !!!
return;
}
for (int i = index; i < candidates.size(); i++)
{
if (candidates[i] <= target)
{
// Judge whether the newly added element is equal to the last fallback element
/* ** The principle here lies in the following dfs()index The parameter is i+1, After the last recursive return , ** Because after sorting , Of the caller index It may point to the same value as the element being rolled back , Repeat the result , Such as : ** In the example [1,1,2,5,6,7,10],target=8 Example ( Let's assume that sort After that ) in , ** When index = 0 when , It's already matched [1,2,5] Result , When the call is finally traced ( That is, the first call ) after , ** Iterate to index = 1 when , The value at this position is still 1, And because of ascending sort , If not at this time index=1 exclude , ** Then it can still match another one with the subsequent elements [1,2,5] Result . */
if (candidates[i] == back) {
// Skip the same element as the last time
continue;
}
combine.emplace_back(candidates[i]);// Push
dfs(candidates, target - candidates[i], ans, combine, i + 1);// Backtracking operations
back = combine.back();// Access the top element of the stack
combine.pop_back();// eject
}
else
break;
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;// Result array
vector<int> combine;// Lists that have been combined
sort(candidates.begin(), candidates.end());// Ascending order
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;
}
边栏推荐
- gcc -l参数和-L参数的区别
- QT notes - custom data types
- 一文看懂MES系统能实现企业哪些目标
- Microsoft SQL Server database language and function usage (XII)
- CCF 1-2 question answering record (1)
- JS image to Base64
- Markdown mathematical formula syntax
- Counter attack dark horse: devdbops training, give you the best courses!
- Three small knowledge points about data product managers
- Repeated calls, messages, idempotent schemes, full collation
猜你喜欢

源码分析Sentry用户行为记录实现过程

Online XML to CSV tool

第1章 引言

Do you regret learning it?

Equal principal increasing repayment / equal principal decreasing mortgage repayment calculator

Counter attack dark horse: devdbops training, give you the best courses!

C Advanced - data storage
![Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]](/img/ee/e0770298d0534014485145c434491a.png)
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]

NFT digital collection system construction - app development

TypeNameExtractor could not be found
随机推荐
[I also want to brush through leetcode] 468. Verify the IP address
Agile? DevOps ?
C # entry series (29) -- preprocessing commands
Markdown mathematical formula syntax
Open source DNS software powerdns BIND9 mydns
TypeNameExtractor could not be found
NFT digital collection system construction - app development
GCC的基本用法
leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
Use and expansion of fault tolerance and fusing
Threat hunting collection
Most after analyze table in PostgreSQL_ common_ Why is the elems field not filled in?
三层交换机配置MSTP协议详解【华为eNSP实验】
MySQL advanced (XVII) cannot connect to database server problem analysis
【网络空间安全数学基础第9章】有限域
try...finally总结
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
Day4: circular structure
Zhihuihuayun | cluster log dynamic collection scheme
理解数据的存与取