Leetcode题解(4):L216/Combination Sum III
L216: 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]]
解题思路:递归,注意一些条件的处理,避免不必要的处理
class Solution {
public:
bool combine(int min, int k, int n, vector<int> vec, vector<vector<int>>& result){
if(n<min*k) //更确切的。能够用等差数列公式算出最大值最小值
return false;
if(n>9*k)
return true;
if(k==1)
{
vec.push_back(n);
result.push_back(vec);
return true;
}
for(int i=min;i<=9;i++)
{
vec.push_back(i);
bool rt = combine(i+1, k-1,n-i, vec, result);
vec.pop_back();
if(!rt)
break;
}
return true;
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> result;
vector<int> vec;
combine(1,k,n,vec,result);
return result;
}
};
Leetcode题解(4):L216/Combination Sum III的更多相关文章
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- [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. 组合总和 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 ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【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】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 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 ...
随机推荐
- Springmvc web项目初始化
Web容器首先会读取项目中的web.xml配置文件中的两个节点:<context-param>与<listener> Web容器创建ServletContext对象即Servl ...
- Vmware占用宿主机硬盘越来越大
Vmware占用宿主机硬盘越来越大 root /usr/bin/vmware-toolbox-cmd disk shrink /
- 二进制<2>
位运算简介及实用技巧(二):进阶篇(1) ===== 真正强的东西来了! ===== 二进制中的1有奇数个还是偶数个 我们可以用下面的代码来计算一个32位整数的二进制中1的个数的奇偶性, ...
- php中 ob_start()有什么作用
<?php ob_start(); //开启缓冲区 echo "这是第一次输出内容!\n"; $ff[1] = ob_get_contents() ; //获取当前缓冲区内容 ...
- animation总结
1. animation结束后停在最后一帧 animation-fill-mode : forwards | both; /* 或者 */ animation: anim1 1s linear for ...
- mybatis学习(八)——resultMap之association&&collection解析
一.resultMap的使用 resultMap 也是定义返回值类型,返回值为用户自定义的类型,可用于解决JavaBean中的属性名和数据库中的列名不一致的情况 之前对于JavaBean中属性名和数据 ...
- iOS-通信录
1.概述: * 对于每一个移动设备而言,都有一个内置的数据库-----通讯录. * 在IOS上,通讯录放在SQLite3数据库中. * 由于不同应用之间不能直接访问,我们想要实现对数据库的访问,必须使 ...
- vs nuget package control.
关于nuget,貌似使用nuget获取的package会在项目解决方案根目录下面将所有download下来的依赖包存储下来,所以这里的package会是最后所有的引用所在,既然不自己维护dll库的位置 ...
- wpf LookUpEdit PopupContentTemplate
<dxg:LookUpEdit Name="searchLookUpEdit" HorizontalAlignment="Stretch" PopupHe ...
- Linux 之 用户及用户组
用户及用户组 参考教程:[千峰教育] 命令: whoami: 作用:查看当前登录的用户. 格式:whoami /etc/passwd: 说明:该文件存放了系统中所有的用户,每一行的每一列如下: 用户名 ...