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. libevent源码学习(15):信号event的处理

    目录信号event处理流程与信号event相关的结构体初始化工作创建一个信号event添加一个信号event信号回调函数信号event的激活       Libevent中的event,主要分为三大类 ...

  2. docker安装artemis

    Dockerfile # Licensed to the Apache Software Foundation (ASF) under one # or more contributor licens ...

  3. JAVA使用IDEA本地调试服务的代码

    然后将启动参数的 jdwp=transport=dt_socket,server=y,suspend=n,address=8086 放到服务器上 在执行jar包的命令加入这个 例如 java -jar ...

  4. IDEA报错 Unable to open debugger port (127.0.0.1:63342): java.net.BindException "Address already in use

    Unable to open debugger port (127.0.0.1:63342): java.net.BindException  "Address already in use ...

  5. 【LeetCode】997. Find the Town Judge 解题报告(C++)

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

  6. Rikka with Graph(hdu5631)

    Rikka with Graph  Accepts: 123  Submissions: 525  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  7. [炼丹术]UNet图像分割模型相关总结

    UNet图像分割模型相关总结 1.制作图像分割数据集 1.1使用labelme进行标注 (注:labelme与labelImg类似,都属于对图像数据集进行标注的软件.但不同的是,labelme更关心对 ...

  8. [C/C++]linux下c-c++语法知识点归纳和总结

    1.c/c++申请动态内存 在c++中,申请动态内存是使用new和delete,这两个关键字实际上是运算符,并不是函数. 而在c中,申请动态内存则是使用malloc和free,这两个函数是c的标准库函 ...

  9. [opencv]拟合vector<Mat>集合区域接近的元素

    vector<Rect> PublicCardFrameDetection::fitrect(vector<Rect> rects){ int size = rects.siz ...

  10. BL8810|USB2.0高速闪存读卡器芯片|BL8810规格书

    1.说明 BL8810是一款USB 2.0读卡器控制器,采用高度集成的单芯片解决方案,旨在提供USB2.0和SD.SDHC.mini SD.Micro SD(T-Flash)接口规范之间的高速数据传输 ...