216. 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.
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]]
题目:从1-9个数字中,找出k个数,是k个数的和为n,
返回所有k个数字所有的组合,每一个组合中k个数字都是不相同的.
思路:
深度优先搜索dfs+剪枝,
leetecode上的大部分递归题目,都会利用这种方式的写法
===============
code:
时间复杂度:O(n!),会出现n!总组合
空间复杂度:O(n),递归会n层
class Solution {
public:
void help_cbs3dfs(int k,int n,int level,vector<int> &out,vector<vector<int>> &re){
//@k,每一组合的数量
//@n,
//@level,从何处开始搜索
//@out,保存搜索路径
//@re,最终结果
if(n<) return;
if(n== && (int)out.size()==k) re.push_back(out);
for(int i = level;i<=;i++){
out.push_back(i);
help_cbs3dfs(k,n-i,i+,out,re);
out.pop_back();
}
}
vector<vector<int>> combinationSum3(int k,int n){
vector<vector<int>> re;
vector<int> out;
help_cbs3dfs(k,n,,out,re);
return re;
}
};
==
216. Combination Sum III的更多相关文章
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [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 ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 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 ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- Java for LeetCode 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
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 216. Combination Sum III——本质DFS
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- clientX,screenX,pageX,offsetX的异同 【转载】
首先说明一下以上对象都是指javascript中的,不包含其他语言. pageX/pageY: 鼠标相对于整个页面的X/Y坐标.注意,整个页面的意思就是你整个网页的全部,比如说网页很宽很长,宽2000 ...
- socket 简介
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- SQL注入测试平台 SQLol -3.INSERT注入测试
访问首页的insert模块,http://127.0.0.1/sql/insert.php,开始对insert模块进行测试. insert语句: INSERT INTO [users] ([usern ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- P235 实战练习(集合类)
将1~100之间的所有正整数存放在一个List集合中,并将集合中索引位置是10的对象从集合中移除. package org.hanqi.practise; import java.util.*; pu ...
- C中的基本数据类型和变量
C语言中的数据类型 基本数据类型 1) 整型 (int %d) 2) 字符型 (char %c) 3) 浮点型 %d ①. 单精度浮点型(float) ②. 双精度浮点型(double) 2.指 ...
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
- Java——设计模式(装饰模式_IO)
/* * 装饰设计模式: * 对一组对象的功能进行增强时,就可以使用该模式进行问题的解决; * 装饰和继承都能实现一样的特点: 就是进行功能的扩转增强. * */ public class ...
- 论文笔记之:Semi-Supervised Learning with Generative Adversarial Networks
Semi-Supervised Learning with Generative Adversarial Networks 引言:本文将产生式对抗网络(GAN)拓展到半监督学习,通过强制判别器来输出类 ...
- stl的仿函数adapter
Stl的一点思考 编程语言是为编译器写一份策略,如果将这份策略模板化那就是泛型编程了 bind1st bind2nd not1 not2 adapter并不改变仿函数接口,只是将参数引入其他的运算流程