当前位置:网站首页>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;
}
}
边栏推荐
- PostgreSQL 存储结构浅析
- Solution to the problem that the keypad light does not light up when starting up
- 芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
- Zabbix2.2监控之系统及应用日志监控报警
- IM即时通讯开发万人群聊消息投递方案
- In the era of super video, what kind of technology will become the base?
- Principle of SSM framework
- Bugku's file contains
- There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
- 模板引擎Velocity 基础
猜你喜欢
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
Authentication processing in interface testing framework
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
Kali install Nessus
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
She is the "HR of others" | ones character
Where should older test / development programmers go? Will it be abandoned by the times?
毕业后5年,我成为了年薪30w+的测试开发工程师
【Hot100】17. Letter combination of telephone number
随机推荐
【直播预约】数据库OBCP认证全面升级公开课
Analysis of PostgreSQL storage structure
Origin2018 installation and use (sorting)
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
How does go use symmetric encryption?
Rhcsa Road
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
ABAP call restful API
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
Which MySQL functions are currently supported by tablestore in table storage?
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
2022 Moonriver global hacker song winning project list
【Hot100】19. 删除链表的倒数第 N 个结点
【Hot100】20. Valid parentheses
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)