当前位置:网站首页>Leetcode 216 combined summation III -- backtracking method
Leetcode 216 combined summation III -- backtracking method
2022-07-01 16:35:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/combination-sum-iii
The question :
Find the sum of all the sums n Of k Combination of numbers . Only allowed in combination 1 - 9 The positive integer , And there are no duplicate numbers in each combination .
explain :
All numbers are positive integers .
A solution set cannot contain duplicate combinations .
Example 1:
Input : k = 3, n = 7
Output : [[1,2,4]]
Example 2:
Input : k = 3, n = 9
Output : [[1,2,6], [1,3,5], [2,3,4]]
Ideas :
In fact, if you do 《LeetCode 77 Combine 》 This question , This question is actually easier . It is suggested that this problem 《LeetCode 77 Combine 》 Just do it , They all use the backtracking method and do simple pruning .
The pruning of this topic is reflected in for In circulation sum+i<=n
On this conditional statement .
This topic Java Code :
class Solution {
int sum;
List<List<Integer>> ans = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
private void backTracking(int k, int n, int start) {
if (path.size() == k) {
if (sum == n) ans.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= 9 && sum + i <= n; i++) {
path.add(i);
sum += i;
backTracking(k, n, i + 1);
sum -= i;
path.removeLast();
}
}
public List<List<Integer>> combinationSum3(int k, int n) {
backTracking(k, n, 1);
return ans;
}
}
边栏推荐
- [nodemon] app crashed - waiting for file changes before starting... resolvent
- 数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)
- Do280 management application deployment - pod scheduling control
- Rhcsa Road
- 博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
- [JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
- VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
- Advantages, values and risks of chain games compared with traditional games
- 你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
- In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
猜你喜欢
The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
Buuctf gold III
Basic use of MySQL
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
广东用电量大跌,说明高新技术产业替代高能耗产业已取得初步成果
动作捕捉系统用于苹果采摘机器人
Problems encountered in IM instant messaging development to maintain heartbeat
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
随机推荐
Comprehensively view the value of enterprise digital transformation
Preliminary study on golang crawler framework
How to use MySQL language for row and column devices?
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
The picgo shortcut is amazing. This person thinks exactly the same as me
毕业后5年,我成为了年薪30w+的测试开发工程师
【Hot100】17. 电话号码的字母组合
游戏行业安全选择游戏盾,效果怎么样?
【Hot100】19. Delete the penultimate node of the linked list
Learn selenium to simulate mouse operation, and you can be lazy a little bit
Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
PostgreSQL 存储结构浅析
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
She is the "HR of others" | ones character
How to write good code - Defensive Programming Guide
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
从大湾区“1小时生活圈”看我国智慧交通建设
制造业数字化转型究竟是什么