当前位置:网站首页>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;
}
}
边栏推荐
- Basic use of MySQL
- Go 语言怎么使用对称加密?
- The sharp drop in electricity consumption in Guangdong shows that the substitution of high-tech industries for high-energy consumption industries has achieved preliminary results
- How to use phpipam to manage IP addresses and subnets
- Why is the pkg/errors tripartite library more recommended for go language error handling?
- Go 语言源码级调试器 Delve
- Motion capture system for apple picking robot
- [SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
- [nodemon] app crashed - waiting for file changes before starting... resolvent
- Principle of SSM framework
猜你喜欢

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)

Problems encountered in IM instant messaging development to maintain heartbeat
![[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境](/img/0e/52e37527bc717c7a55741725087bad.png)
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境

Learn selenium to simulate mouse operation, and you can be lazy a little bit

Comprehensively view the value of enterprise digital transformation

China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District

Where should older test / development programmers go? Will it be abandoned by the times?

Bugku's file contains

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
随机推荐
How to use phpipam to manage IP addresses and subnets
Example of vim user automatic command
实现数字永生还有多久?元宇宙全息真人分身#8i
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
Zabbix2.2监控之系统及应用日志监控报警
VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v
In the era of super video, what kind of technology will become the base?
Research on multi model architecture of ads computing power chip
Which MySQL functions are currently supported by tablestore in table storage?
用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
ssm框架原理
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
红队第10篇:coldfusion反序列化过waf改exp拿靶标的艰难过程
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
UML旅游管理系统「建议收藏」
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
Endeavouros mobile hard disk installation