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 ...
随机推荐
- 【转载】FPGA静态时序分析——IO口时序
转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/01/FPGA.html 1.1 概述 在高速系统中FPGA时序约束不止包括内部时钟约束 ...
- Qt窗体关闭时,如何自动销毁窗体类对象
Qt窗体关闭时,如何自动销毁窗体类对象 要对你的窗口设置WA_DeleteOnClose属性,默认的情况下关闭窗口仅仅意味着隐藏它 ImgWindow1->setAttribute(Qt ...
- 初探Xamarin
Xamarin是一个基于mono的商业项目,收费,而且贼贵.官网地址是:http://xamarin.com/ 就我个人理解,收费的Xamarin提供一个for visual studio 2010/ ...
- Java 集合转换(数组、List、Set、Map相互转换)
转自:http://blog.csdn.net/top_code/article/details/10552827 package com.example.test; import java.util ...
- OC-通讯录
要求描述:用OC语言完成简易通讯录(实现增删改查)功能.(注:使用MRC) 1.创建AddressBook类. 1)使用字典作为容器,字典的Key值为分组名(姓名首字母),value值为数组,数组中存 ...
- IOS- 网络图片缓存到沙盒中 ,离线取出。
一.缓存图片 //1.首先创建在沙盒中创建一个文件夹用于保存图片 NSFileManager *fileManager = [[NSFileManager alloc] init]; NSString ...
- unity工具IGamesTools之批量生成帧动画
unity工具IGamesTools批量生成帧动画,可批量的将指定文件夹下的帧动画图片自动生成对应的资源文件(Animation,AnimationController,Prefabs) unity工 ...
- windows下的node-canvas历程
背景:由于在前期开发的过程中,对前端的小图片采用了css-sprite,开始的时候都是在http://spritegen.website-performance.org/站点上合成图片及样式的,但是某 ...
- LintCode-Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 一些 Shell 脚本(持续更新)
1. 启动日志分析 启动日志格式如下: 开机时间:2015/05/13 周三 16:45:17.79 关机时间:2015/05/13 周三 18:46:03.91 开机时间:2015/05/14 周四 ...