当前位置:网站首页>Combination sum III of leetcode topic analysis
Combination sum III of leetcode topic analysis
2022-06-23 08:59:00 【ruochen】
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Ensure that numbers within the set are sorted in ascending order.
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]
This time, the number of elements is specified , Just record with one variable .
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> rt = new ArrayList<List<Integer>>();
if (k < 1 || n < 1) {
return rt;
}
List<Integer> cur = new ArrayList<Integer>();
dfs(rt, cur, 0, k, n, 1);
return rt;
}
private void dfs(List<List<Integer>> rt, List<Integer> cur, int sum, int k,
int n, int level) {
if (sum == n && k == 0) {
rt.add(new ArrayList<Integer>(cur));
return;
} else if (sum > n || k <= 0) {
return;
}
for (int i = level; i <= 9; i++) {
cur.add(i);
dfs(rt, cur, sum + i, k - 1, n, i + 1);
cur.remove(cur.size() - 1);
}
}边栏推荐
- Longest substring without repeated characters (C language)
- MySQL fault case | error 1071 (42000): specified key was too long
- 986. Interval List Intersections
- 多线程初学
- Quartz Crystal Drive Level Calculation
- Leetcode topic analysis h-index II
- (resolved) difference between leftmost prefix and overlay index
- The fourth online workshop review
- '教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
- Unity grid programming 08
猜你喜欢

Point cloud library PCL from introduction to mastery Chapter 10

Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent

JSP入门总结

Simple student management

测试-- 自动化测试selenium(关于API)

js 用**遮罩身份证以及手机号的重要数据

自定义标签——jsp标签增强
![[cloud native | kubernetes] kubernetes principle and installation (II)](/img/db/dd93bbcac6d0404d44f67d2da12880.png)
[cloud native | kubernetes] kubernetes principle and installation (II)

Custom tags - JSP tag enhancements

JS mask important data of ID card and mobile phone number with * *
随机推荐
On the light application platform finclip and the mobile application development platform mpaas
Detailed explanation of srl16e in xilinxffpga
New engine, new capability, new experience, Tencent host security flagship release
[cloud native | kubernetes] kubernetes principle and installation (II)
测试-- 自动化测试selenium(关于API)
Leetcode topic analysis spiral matrix II
node request模塊cookie使用
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed.
636. Exclusive Time of Functions
Community article | mosn building subset optimization ideas sharing
6、 Web Architecture Design
Leetcode topic analysis set matrix zeroes
Only 187 bytes of desktop dream code
Custom tag - JSP tag Foundation
"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
Kernel log debugging method
173. Binary Search Tree Iterator
JSP入门总结
Vue3表单页面利用keep-alive缓存数据的一种思路
USB peripheral driver - configfs