作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/combination-sum-iii/description/

题目描述:

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:

  1. All numbers will be positive integers.
  2. 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]]

题目大意

只是用1~9这几个数字,而且每个数字只能使用一次,要用k个不同的数字组成和为n的组合,问有多少中不同的组合方式。

解题方法

方法一:DFS

这是这个系列的第三个题,同样使用回溯法去做。这个题的不同之处在于k,n的可变性。所以只有两者同时满足等于零的条件的时候才是满意的结果。另外注意题目中给的范围是1-9的数字,所以缩小了范围。

class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
res = []
self.dfs(xrange(1, 10), k, n, 0, res, [])
return res def dfs(self, nums, k, n, index, res, path):
if k < 0 or n < 0:
return
elif k == 0 and n == 0:
res.append(path)
return
for i in xrange(index, len(nums)):
self.dfs(nums, k - 1, n - nums[i], i + 1, res, path + [nums[i]])

方法二:回溯法

使用回溯法,方法和39题基本一样,唯一的区别是这个题不允许数字多次使用,所以每次循环开始的时候,都要比上一轮大1.

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
helper(res, {}, k, n, 0);
return res;
}
void helper(vector<vector<int>>& res, vector<int> path, int k, int n, int start) {
if (n < 0) return;
if (k == 0 && n == 0) res.push_back(path);
for (int i = start + 1; i <= 9; i ++) {
path.push_back(i);
helper(res, path, k - 1, n - i, i);
path.pop_back();
}
}
};

日期

2018 年 2 月 21 日
2018 年 12 月 20 日 —— 感冒害的我睡不着

【LeetCode】216. Combination Sum III 解题报告(Python & C++)的更多相关文章

  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. 【刷题-LeetCode】216. Combination Sum III

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

  8. LC 216. Combination Sum III

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

  9. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

随机推荐

  1. (转载) IBM DB2数据库odbc配置步骤详解

    [IT168 技术] 首先安装IBM DB2 odbc driver 1):可以单独下载DB2 Run-Time Client,大约(86.6m),安装后则odbc驱动程序安装成功.下载地址:ftp: ...

  2. 网卡命令ifconfig

    • ifconfig • service network restart • ifdown eth0 • ifdown eth0 #linux下run networkexport USER=lizhe ...

  3. Learning Spark中文版--第四章--使用键值对(1)

      本章介绍了如何使用键值对RDD,Spark中很多操作都基于此数据类型.键值对RDD通常在聚合操作中使用,而且我们经常做一些初始的ETL(extract(提取),transform(转换)和load ...

  4. 【转载】HBase基本数据操作详解【完整版,绝对精品】

    转载自: http://blog.csdn.net/u010967382/article/details/37878701 概述 对于建表,和RDBMS类似,HBase也有namespace的概念,可 ...

  5. Flume(三)【进阶】

    [toc] 一.Flume 数据传输流程 重要组件: 1)Channel选择器(ChannelSelector) ​ ChannelSelector的作用就是选出Event将要被发往哪个Channel ...

  6. Hive(十)【窗口函数】

    目录 一.定义 窗口函数: 标准聚合函数 分析排名函数 二.语法 (1)窗口函数 over([partition by 字段] [order by 字段] [ 窗口语句]) (2)窗口语句 三.需求练 ...

  7. flink03-----1.Task的划分 2.共享资源槽 3.flink的容错

    1. Task的划分 在flink中,划分task的依据是发生shuffle(也叫redistrubute),或者是并行度发生变化 1.  wordcount为例 package cn._51doit ...

  8. 在Eclipse中运行OSGI工程出错的解决方案

    今天学习OSGI的过程中按照书上所述搭建好第一个helloworld插件工程,运行的过程中出现下面所示的错误: !SESSION 2014-06-09 21:04:49.038 ----------- ...

  9. RocketMQ应用及原理剖析

    主流消息队列选型对比分析 基础项对比 可用性.可靠性对比 功能性对比 对比分析 Kafka:系统间的流数据通道 RocketMQ:高性能的可靠消息传输 RabbitMQ:可靠消息传输 RocketMQ ...

  10. JSP 文字乱码、${}引用无效

    问题: 代码:<form action="/test/requestPost.do" method="post"> <input type=& ...