1. 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.

Note:

  • All numbers will be positive integers.
  • The solution set must not contain duplicate combinations.

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]]

定义函数f(l, r, n, target),表示在区间[l, r]之间和为target的n个数,则:

\[f(l, r, n, target) = \bigcup_{i=l}^r f(i+1, r, n-1, target - i)
\]

当n=2时,直接采用双指针搜索

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<bool>nums(10, true);
return comb(1, 9, k, n);
}
vector<vector<int>> comb(int l, int r, int n, int target){
vector<vector<int>>ans;
if(n == 2){
int i = l, j = r;
while(i < j){
int tmp = i + j;
if(tmp == target){
ans.push_back({i,j});
i++;
j--;
}else if(tmp < target){
i++;
}else{
j--;
}
}
return ans;
}
for(int i = l; i <= r; ++i){
vector<vector<int>>tmp_ans = comb(i+1, r, n - 1, target - i);
if(tmp_ans.size() > 0){
for(auto &v : tmp_ans){
v.insert(v.begin(), i);
ans.push_back(v);
}
}
}
return ans;
}
};

【刷题-LeetCode】216. Combination Sum III的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. LeetCode 216. Combination Sum III (组合的和之三)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  7. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

随机推荐

  1. CF390A Inna and Alarm Clock 题解

    Content 平面内有 \(n\) 个整点 \((x_i,y_i)\).每次可以去掉一行或一列的点,问最少去几次(按行去点或按列去点二者只能选择一种). 数据范围:\(1\leqslant n\le ...

  2. LuoguB2035 判断数正负 题解

    Content 给定一个数 \(n\),判断这个数的正负. 数据范围:\(-10^9\leqslant n\leqslant 10^9\). Solution 一个基础的分支结构题. if-else ...

  3. vue 判断页面是否滚动到底部

    需求 要求用户阅读完本页所有内容后,下一步按钮才可以点击. 实现思路 通过判断当前页面是否到达底部来设置按钮的点击事件. 要判断当前页面是否到达底部需要用到三个距离--距离顶部的距离scrollTop ...

  4. JAVA获取当前日期的下周一到下周日的所有日期集合

    /** * 获取当前日期的下周一到下周日的所有日期集合 * @return */ public static List getNextWeekDateList(){ Calendar cal1 = C ...

  5. 【LeetCode】246. Strobogrammatic Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  6. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

  7. 【九度OJ】题目1195:最长&最短文本 解题报告

    [九度OJ]题目1195:最长&最短文本 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串, ...

  8. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. python学习第一天:window安装python开发环境完整篇

    我是跟着廖雪峰老师的的博客来一步一步来进行学习和实践后记录下来的,讲的非常地详细,推荐大家一起学习https://www.liaoxuefeng.com/wiki/0014316089557264a6 ...

  10. Kronecker Products and Stack Operator

    目录 定义 Stack Operator Kronecker Product 性质 Stack Operator Kronecker Product 半线性 Whitcomb L. Notes on ...