题意:

  在1~9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序。

思路:

  用DFS的方式,每次决定一个数字,共决策k次。假设上个决策是第i位为5,那么i+1位的范围就是6~9。

 class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ans;
vector<int> num(k,);
DFS(ans,num,k,,,,n);
return ans;
}
void DFS(vector<vector<int>>& ans,vector<int>& num,int k,int j,int i,int sum,int n)
{
if(i==k)
{
if(sum==n) ans.push_back(num);
return ;
}
else
{
for(j++; j<; j++)
{
num[i]=j;
DFS(ans,num,k,j,i+,sum+j,n);
}
}
}
};

AC代码

LeetCode Combination Sum III (DFS)的更多相关文章

  1. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  2. 【leetcode】Combination Sum III(middle)

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

  3. [LeetCode] Combination Sum II (递归)

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

  4. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  5. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  6. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  7. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  8. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  9. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

随机推荐

  1. 用js实现返回上一页

    <a href="javascript :;" onClick="javascript :history.back(-1);">返回上一页</ ...

  2. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. Oracle 权限查询

    查看当前用户权限:SQL> select * from session_privs; 查询某个用户被赋予的系统权限. Select * from user_sys_privs; 或者: sele ...

  4. 通过电脑(计算机)下载google play的应用的方法

    手机上安装GOOGLE PLAY很麻烦,经常不成功.于是采用电脑下载APK的方法. 网上传说的下载chrome的插件,然后获取DEVICE ID的方法,经过试用,一直无法获得DEVICE ID. 经过 ...

  5. js基础之动画(三)

    一.链式运动 首先,要改进运动框架 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; ...

  6. POJ 2253 Frogger 最短路 难度:0

    http://poj.org/problem?id=2253 #include <iostream> #include <queue> #include <cmath&g ...

  7. WP8 学习 在APP.XAML中加入Resources

    <Application.Resources> <local:LocalizedStrings xmlns:local="clr-namespace:test1" ...

  8. Android Support ;v4、v7、v13的区别

    google提供了Android Support Library package系列的包来保证来高版本sdk开发的向下兼容性,即我们用4.x开发时,在1.6等版本上,可以使用高版本的有些特性,如fra ...

  9. Linux Program

    vhost : web.myftp.com storage.myftp.com ...... not vhost : storage.myftp.com eg : top eg : htop eg : ...

  10. 解决maven Generating project in Interactive mode卡死问题(转)

    原文链接:http://blog.csdn.net/only_wan/article/details/52975760 mvn 创建时在generating project in interactiv ...