当前位置:网站首页>LeetCode_ Backtracking_ Medium_ 216. Combined sum III
LeetCode_ Backtracking_ Medium_ 216. Combined sum III
2022-07-26 20:51:00 【Old street of small town】
1. subject
Find the sum of all the sums n Of k Combination of numbers , And the following conditions are met :
① Use only numbers 1 To 9
② Each number can be used at most once
Returns a list of all possible valid combinations . The list cannot contain the same combination twice , Combinations can be returned in any order .
Example 1:
Input : k = 3, n = 7
Output : [[1,2,4]]
explain :
1 + 2 + 4 = 7
There is no other matching combination .
Example 2:
Input : k = 3, n = 9
Output : [[1,2,6], [1,3,5], [2,3,4]]
explain :
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There is no other matching combination .
Example 3:
Input : k = 4, n = 1
Output : []
explain : There is no valid combination .
stay [1,9] Use... In scope 4 A different number , The minimum sum we can get is 1 + 2 + 3 + 4 = 10, because 10 > 1, No valid combination .
Tips :
2 <= k <= 9
1 <= n <= 60
source : Power button (LeetCode)
link :https://leetcode.cn/problems/combination-sum-iii
2. Ideas
(1) to flash back
This question is very similar to the following two questions , Just modify the previous code slightly .
LeetCode_ to flash back _ secondary _39. Combinatorial summation
LeetCode_ to flash back _ secondary _40. Combinatorial summation II
3. Code implementation (Java)
// Ideas 1———— to flash back
class Solution {
// res Used to save the final results
List<List<Integer>> res = new ArrayList<>();
// track Path used to record backtracking
ArrayList<Integer> track = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
int[] candidates = {
1, 2, 3, 4, 5, 6, 7, 8, 9};
int minSum = 0;
int maxSum = 0;
for (int i = 0; i < k; i++) {
minSum += candidates[i];
maxSum += candidates[candidates.length - i - 1];
}
// Judgement boundary
if (n > maxSum || n < minSum) {
return res;
}
backtrack(candidates, 0, n, 0, k);
return res;
}
public void backtrack(int[] candidates, int start, int target, int sum, int k) {
if (sum == target && track.size() == k) {
// Find target combination , Save it to res in
res.add(new ArrayList<>(track));
return;
}
if (sum > target || track.size() > k) {
// The sum of combined elements exceeds target Or use more than k, End directly
return;
}
// Backtracking algorithm framework
for (int i = start; i < candidates.length; i++) {
// To make a choice
sum += candidates[i];
track.add(candidates[i]);
/* backtrack( route , Selection list ) It should be noted that , Array candidates Elements in can only be used once , So when looking down in the backtracking tree , Starting from i + 1 */
backtrack(candidates, i + 1, target, sum, k);
// Unselect
sum -= candidates[i];
track.remove(track.size() - 1);
}
}
}
边栏推荐
- What is the origin of CNEX labs, which let Huawei lose the lawsuit?
- Software testing - development test content specification (project test template)
- Centos7 about Oracle RAC 11gr2 deployment disk partition
- Leetcode-300 longest increasing subsequence
- 884. Uncommon words in two sentences - hash table
- 7.25模拟赛总结
- Experiment 6 BGP federal comprehensive experiment
- 李彦宏遭“泼冷水”热情不减!百度结盟华为麒麟,发布“鸿鹄”芯片
- Common operations of ndarray in numpy
- Transaction rollback and record exception information at the same time
猜你喜欢

Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions

如何组装一个注册中心?

Chat software project development 2

Use Baidu PaddlePaddle easydl to complete garbage classification

BTC和ETH不确定性增强 因加息逼近?美国经济将面临更多痛苦

深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积

分组卷积(Group Converlution)

Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业
![[record of question brushing] 22. bracket generation](/img/0d/8881fcbcd0e963875dff2946b95865.png)
[record of question brushing] 22. bracket generation

Swiftui 4's new function of real-time access to click location.Ontapgeture {location in} (tutorial with source code)
随机推荐
Tkinter uses WPF controls
Bean injection and lifecycle
leetcode 哈希表类
Fitting the new direction of curriculum standards, ape guidance, creating a characteristic new concept content system
serializable接口的作用是什么?
South Korea plans to spend 1 trillion won a year on research and development of semiconductor materials and equipment
How to check whether the pytorch you are using is a GPU version
LeetCode链表问题——24.两两交换链表中的结点(一题一文学会链表)
Green and sustainable development of data center
易基因|宏病毒组测序技术介绍
TinUI发展历程
BUU刷题记3
"Enterprise management" sincere crm+ - integrated management of enterprise business processes
李彦宏遭“泼冷水”热情不减!百度结盟华为麒麟,发布“鸿鹄”芯片
实验5 OSPF综合实验
Chat software project development 2
How to assemble a registry?
[record of question brushing] 22. bracket generation
Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
Easy gene | introduction to macrovirus sequencing technology