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. webservice注意事项

    1.private static final QName PORT_NAME = new QName("http://server.helloworld.cxf.demo/",&q ...

  2. 【JAVA今法修真】 第六章 天道无情,锁定乾坤

    您好,我是南橘,万法仙门的掌门,刚刚从九州世界穿越到地球,因为时空乱流的影响导致我的法力全失,现在不得不通过这个平台向广大修真天才们借去力量.你们的每一个点赞,每一个关注都是让我回到九州世界的助力,兄 ...

  3. JAVA获得websocket请求路径前缀

    /** * 获得websocket请求路径前缀(线程安全 速度相对慢) * @param request * @return */ public static String getWebsocketU ...

  4. JAVA导入(读取)Excel中的数据(支持xls与xlsx文件)

    一.导入jar包 poi-3.7.jarpoi-scratchpad-3.7.jarpoi-examples-3.7.jarpoi-ooxml-3.7.jarpoi-ooxml-schemas-3.7 ...

  5. JAVA判断是否是移动端设备(手机和平板)访问

    import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 判断是否为移动端设备访问 * */ public class ...

  6. Mac下好用的“visio”之 OmniGraffle Pro

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 1.官方网站:https://www.omnigroup.com/omnigraffle/ ...

  7. 网络编程之新函数inet_pton和inet_ntop

    1.头文件 1 #include <arpe/inet.h> 2.inet_pton 函数 A.原型 1 int inet_pton(int family, const char *str ...

  8. 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

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

  9. 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)

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

  10. Class Activation Mapping (CAM)

    目录 概 主要内容 CAM Grad-CAM Grad-CAM++ Score-CAM 最后 代码 Zhou B., Khosla A., Lapedriza A., Oliva A. and Tor ...