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 ...
随机推荐
- [转载]oracle 11g不能导出空表的多种解决方法
原文地址:11g不能导出空表的多种解决方法">oracle 11g不能导出空表的多种解决方法作者:Anlin_Yang ORACLE 11g 用exp命令导出库文件备份时,发现只能导出 ...
- Apache Flex + Adobe Flash Builder环境配置
在开始学习Flex之前,需要配置开发环境.Apache Flex SDK包含了你开发所需要的东西,当然除了集成开发环境(Integrated Development Environment,IDE). ...
- 设置SAPgui自动退出功能
当客户端长时间没有使用时,为了不占用SAP服务器的资源,我们可以设置当客户端在一段时间没有 进行操作时自动退出SAP系统. 设置步骤如下: 用Tcode rz10打开参数配置窗口,选择你当前的实例参数 ...
- Spring-IOC之前世今生
没使用IOC之前 贯用工厂模式调用 bean组件 可类比为: 去商场买东西,你是先想好自己要买什么了,然后去寻找商品(逛商场的同学不要扯皮啊2333) 用了IOC之后 可类比为: 所有网上购物,所有的 ...
- this指针基础介绍
=================this指针的由来==================== 一个学生可以有多本书一样,而这些书都是属于这个同学的:同理,如果有很多个同学在一起,那么为了确定他们的书不 ...
- Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- C++ Primer : 第十二章 : 动态内存之shared_ptr类实例:StrBlob类
StrBlob是一个管理string的类,借助标准库容器vector,以及动态内存管理类shared_ptr,我们将vector保存在动态内存里,这样就能在多个对象之间共享内存. 定义StrBlob类 ...
- thinkPHP 中去除URL中的index.php
例如你的原路径是 http://localhost/app/index.php/module/action/var/value/ 那么现在的地址是 http://localhost/app/modul ...
- codeForce-19D Points (点更新+离散化)
题目大意:在二维坐标系的x正半轴,y正半轴和第一象限内,有三种操作: 1.add x,y (添加点<x,y>): 2.remove x,y(移除点<x,y>): 3.find ...
- hdu1811 并查集+拓扑序
题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能 ...