Combination Sum III
https://leetcode.com/problems/combination-sum-iii/
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]]
解题思路:
新题,与前面的系列相比,简单了一些,常规的dfs,没有啥好说的。
public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
dfs(k, n, res, cur, 1, 0);
return res;
}
public void dfs(int k, int n, List<List<Integer>> res, List<Integer> cur, int step, int sum) {
if(cur.size() == k && sum == n) {
res.add(new ArrayList(cur));
return;
}
if(cur.size() > k) {
return;
}
if(cur.size() <= k && sum > n) {
return;
}
for(int i = step; i < 10; i++) {
cur.add(i);
sum += i;
dfs(k, n, res, cur, i + 1, sum);
cur.remove(cur.size() - 1);
sum -= i;
}
}
}
//20181013
class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
dfs(res, cur, k, n, 1, 0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> cur, int k, int n, int start, int sum) {
if (cur.size() == k && sum == n) {
res.add(new ArrayList(cur));
}
for (int i = start; i <= Math.min(n, 9); i++) {
cur.add(i);
dfs(res, cur, k, n, i + 1, sum + i);
cur.remove(cur.size() - 1);
}
}
}
Combination Sum III的更多相关文章
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- Leetcode题解(4):L216/Combination Sum III
L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, giv ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- hdu 5210 Delete
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5210 简单题如下: #include<algorithm> #include<ios ...
- bash: 避免命令重复执行的简单脚本
1. 根据命令生成md5做为文件名保存当前进程的pid2. 使用exec执行命令3. 如果再次执行, 使用ps -p检测上次pid是否有效, 如果是则exit 200.否则重复1.hadoop@ubu ...
- sychronized面试问题浅析
先说下面试吧,整体来说基础准备好点,简历别太假,然后回答起来实事求是,表现自然的点基本上都没问题吧(针对初级职位,记得有个hr说过对于新人基础扎实和为人真诚是最关键的),两天时间跑起来挺累,反而觉得面 ...
- libevent I/O示例
I/O示例使用一个windows平台上服务器/客户端的例子来演示.由于为了减少代码篇幅等各种由于本人懒而产生的原因,以下代码没有做错误处理以及有些小问题,但是我想应该不影响演示,大家多包涵. 服务器代 ...
- qt QSS文件伪状态
表 1. 伪状态列表伪状态 描述:checked button部件被选中:disabled 部件被禁用:enabled 部件被启用:focus 部件获得焦点:hover ...
- 《实时控制软件设计》Git 基本操作练习
根据老师提供的教程 对 数据库创建.提交文件.创建分支.删除分支.合并分支.冲突处理等操作进行了练习 得到log文件如下: yanbin-guo@yanbinguo MINGW64 /Git (mas ...
- 12、android socket使用demo:网络聊天
目录: 一.效果图 二.原代码分享 三.代码分析 四.总结 一.效果图如下: 客户端1: 客户端2: 二.原代码分享如下: 1.java代码只有一个 MainActivity.ja ...
- Mysql的主从数据库没有同步的解决办法
Mysql的主从数据库没有同步的解决办法 今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. ...
- dancing link
http://www.cnblogs.com/grenet/p/3145800.html 链接给的博客写的很好,比较好懂. 可惜不是c语言... 于是决定自己要建一个模板. 一道裸题:hustoj 1 ...
- 异步解压ZIP文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...