前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LintCode Backpack VI题目分析代码

LintCode Backpack VI题目分析代码

作者头像
desperate633
发布2018-08-22 11:46:24
2200
发布2018-08-22 11:46:24
举报
文章被收录于专栏:desperate633desperate633

题目

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

** 注意事项 ** The different sequences are counted as different combinations.

样例 Given nums = [1, 2, 4], target = 4

Paste_Image.png

return 6

分析

这个问题的思路是,4可以由nums里面的2,2组成,那么2又可以由nums里面的什么组成呢?就是分别把每个都作为target计算,结果存入dp[target]

代码

代码语言:javascript
复制
public int backPackVI(int[] nums, int target) {
        // Write your code here
        int[] dp = new int[target+1];
        dp[0] = 1;
        for (int i = 1; i <= target; i++) {
            for (int num: nums) {
                if (num <= i) dp[i] += dp[i-num];
            }
        }
        return dp[target];
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 分析
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档