当前位置:网站首页>LeetCode_216_组合总和Ⅲ
LeetCode_216_组合总和Ⅲ
2022-07-17 15:11:00 【Fitz1318】
题目链接
题目描述
找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
- 只使用数字
1到9 - 每个数字 最多使用一次
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
示例 1:
输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。
示例 2:
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。
示例 3:
输入: k = 4, n = 1
输出: []
解释: 不存在有效的组合。
在[1,9]范围内使用4个不同的数字,我们可以得到的最小和是1+2+3+4 = 10,因为10 > 1,没有有效的组合。
提示:
2 <= k <= 91 <= n <= 60
解题思路
回溯法
有了组合这道题,也就是取数范围限制在【1-9】
回溯法三部曲
- 递归函数的参数和返回值
n:目标之和k:组合数的个数path:用来存放符合条件的结果ans:用来存放符合条件结果的集合
- 终止条件
- 到达叶子节点,也就是
path这个数组的大小达到k,说明已经找到一个子集大小为k的组合了,在图中path存的就是根节点到叶子节点的路径了 - 此时用
ans数组将结果保存起来,并终止本层递归
- 到达叶子节点,也就是
- 单层逻辑
- 用
for循环从startIndex开始遍历,然后用path保存取到的节点i, - 然后判断
path里面的数值相加之和是不是n- 如果是,那么就将这个组合
path加入到答案数组ans中 - 如果不是,证明这个组合不符合答案,舍弃掉
- 如果是,那么就将这个组合
- 用
AC代码
class Solution {
public static List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backTarcing(n, k, 1, path, ans);
return ans;
}
private static void backTarcing(int n, int k, int startIndex, List<Integer> path, List<List<Integer>> ans) {
//终止条件
if (path.size() == k) {
int sum = 0;
for (int i = 0; i < path.size(); i++) {
sum += path.get(i);
}
if (sum == n) {
ans.add(new ArrayList<>(path));
}
return;
}
for (int i = startIndex; i <= 9; i++) {
path.add(i);
backTarcing(n, k, i + 1, path, ans);
path.remove(path.size() - 1);
}
}
}
边栏推荐
- How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
- 《MySQL DBA封神打怪之路》专栏学习大纲
- Dream CMS Front Office Search SQL Injection
- What do you look at after climbing the wall? The most popular foreign website - website navigation over the wall
- Unity high version returned low version error
- Learning note 3 -- basic idea of machine learning in planning control
- Tikv memory parameter performance tuning
- Solution of connecting MySQL instance with public network
- 024.static and final use traps continued
- Wechat applet cloud development 1 - Database
猜你喜欢

TCP congestion control details | 7 Surpass TCP

Discussion on Euler angle solution of rocket large maneuvering motion

Transport layer -------- TCP (I)

2022 National latest fire-fighting facility operator (intermediate fire-fighting facility operator) simulation test questions and answers

【嵌入式单元测试】C语言单元测试框架搭建

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning

Introduction of database lock, shared with InnoDB, exclusive lock

From "passive" to "active", how can zeta technology help to upgrade "rfid2.0"?

02-3. Difference between pointer and reference

NAT technology and NAT alg
随机推荐
TCP拥塞控制详解 | 7. 超越TCP
03-1. Inline function, auto keyword, typeID, nullptr
Chapter 1 of creating virtual machine (vmvare virtual machine)
Huawei firewall authentication technology
公网连接MySQL实例的解决方案
Region 性能调优
Robot development -- robot data summary
From "passive" to "active", how can zeta technology help to upgrade "rfid2.0"?
QT -- excellent open source project
Developing those things: how to solve the problem of long-time encoding and decoding of RK chip video processing?
Dynamic memory allocation problem
Redis分布式緩存-Redis集群
梦想CMS 前台搜索SQL注入
Learning note 3 -- basic idea of machine learning in planning control
Microservice online specification
Leetcode skimming -- find and minimum k-pair number 373 medium
Property analysis of rotate matrix (forwarding)
The basic establishment of the sequence table and the related operations of adding, deleting, modifying and querying (the sequence table described in C language)
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
TCP拥塞控制详解 | 7. 超越TCP