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


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

题目描述

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.

Follow up:

  • What if negative numbers are allowed in the given array?
  • How does it change the problem?
  • What limitation we need to add to the question to allow negative numbers?

题目大意

给了一个只包含正整数且不重复的数组,有多少种和为target的方案。

解题方法

这个题用回溯法竟然超时了!怪不得需要返回个数而不是所有的答案。

我们需要一个一维数组dp,其中dp[i]表示目标数为i的解的个数,然后我们从1遍历到target,对于每一个数i,遍历nums数组,如果i>=x, dp[i] += dp[i - x]。这个也很好理解,比如说对于[1,2,3] 4,这个例子,当我们在计算dp[3]的时候,3可以拆分为1+x,而x即为dp[2],3也可以拆分为2+x,此时x为dp[1],3同样可以拆为3+x,此时x为dp[0],我们把所有的情况加起来就是组成3的所有情况了。

算dp[n]的时候遍历num[1] to num[n] index = i

如果i < dp[n] 那么dp[n] = dp[n] + dp[n-i]

从逻辑上来考虑比较复杂,比如4=0+4 1+3 2+2 3+1 4+0

0+4 1 (1+1+1+1)
1+3 1的组合3的组合
2+2 2的组合2的组合
3+1 3的组合*1的组合
4+0 1 (4)

1+4+4+4+1 然后除以2 因为重复了一遍
但是结果似乎不对

看答案 算法是

dp[n] = dp[n] + dp[n-i]
比如4
从1遍历到4
1<4
dp[4] = dp[4] + dp[4-1]

Python解法如下:

class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp = [0] * (target + 1)
dp[0] = 1
for i in xrange(1, target + 1):
for num in nums:
if i >= num:
dp[i] += dp[i - num]
return dp.pop()

C++代码如下:

class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (auto a : nums) {
if (i >= a) {
dp[i] += dp[i - a];
}
}
}
return dp.back();
}
};

日期

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

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

  1. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. Leetcode 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  4. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

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

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

  6. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

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

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

  8. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

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

  9. 【LeetCode】666. Path Sum IV 解题报告 (C++)

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

随机推荐

  1. IDEA+maven+javafx(java 1.8)入坑记录

    序 好久没写博客了,主要是因为懒,写博客真的是个难坚持的事.但今天登上来看了看,之前记录ctf写的wp竟然点击量这么多了,突然让我有了继续写下去的动力. 这段时间遇到了好多事,中间也有想过写几篇文章记 ...

  2. URLDNS分析

    学习了很久的Java基础,也看了很多的Java反序列化分析,现在也来分析学习哈最基础的URLDNS反序列化吧. Java反序列化基础 为了方便数据的存储,于是乎有了现在的Java序列化于反序列化.序列 ...

  3. 第三个基础框架 — springMVC — 更新完毕

    1.什么是springMVC? 还是老规矩,百度百科一下 这里面说了一堆废话,去官网瞄一下 官网网址:https://docs.spring.io/spring-framework/docs/curr ...

  4. LeetCode子矩形查询

    LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...

  5. 调试器gdb

    1.启动和退出gdb gdb调试的对象是可执行文件,而不是程序源代码.如果要使一个可执行文件可以被gdb调试,那么在使用编译器gcc编译程序时加入-g选项.-g选项告诉gcc在编译程序时加入调试信息, ...

  6. webservice--常用注解

    定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...

  7. "delete this" in C++

    Ideally delete operator should not be used for this pointer. However, if used, then following points ...

  8. 面向切面编程(Spring AOP)

    一.什么是AOP AOP即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.主要体现在日志记录.性能统计.安全控制.事务处理和异常处理等. 1.相关概念 二.切面.切入点配 ...

  9. pandas读取csv文件中文乱码问题

    1.为什么会出现乱码问题,用什么方式编码就用什么方式解码,由于csv不是用的utf-8编码,故不能用它解码. 常用的编码方式有 utf-8,ISO-8859-1.GB18030等. 2.中文乱码原因: ...

  10. 网络协议之:还在用HTTP代理?弱爆了!快试试SOCKS5

    目录 简介 为什么要使用SOCKS SOCKS5 SOCKS5的使用 总结 简介 存在即是合理,SOCKS5的出现是为了解决SOCKS4中不支持身份认证的大问题而出现的,毕竟大家对网络中的安全越来越重 ...