博客
关于我
377. Combination Sum IV
阅读量:254 次
发布时间:2019-03-01

本文共 995 字,大约阅读时间需要 3 分钟。

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.

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/combination-sum-iv
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

dp[i]表示和为i的数量,4,则为单个数字,加上其他的组合数

class Solution {    public int combinationSum4(int[] nums, int target) {        int[]dp = new int[target + 1];        Arrays.sort(nums);        dp[0] = 1;        for (int i = 1; i <= target; i++) {            int num = 0;            for (int j = 0; j < nums.length; j++) {                if (nums[j] <= i) {                    num += dp[i - nums[j]];                } else break;            }            dp[i] = Math.max(dp[i], num);        }        return dp[target];    }}

 

你可能感兴趣的文章
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>