码迷,mamicode.com
首页 > 其他好文 > 详细

Backpack V

时间:2019-12-21 22:50:22      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:++i   sample   items   inline   ret   +=   ber   lin   down   

Description

Given n items with size nums[i] which an integer array and all positive numbers. An integer target denotes the size of a backpack. Find the number of possible fill the backpack.

Each item may only be used once

Example

Given candidate items [1,2,3,3,7] and target 7,

A solution set is: 
[7]
[1, 3, 3]

return 2

public class Solution {
    /**
     * @param nums: an integer array and all positive numbers
     * @param target: An integer
     * @return: An integer
     */
     public int backPackV(int[] nums, int target) {
        // Write your code here
        int[] f = new int[target + 1];
        f[0] = 1;
        for (int i = 0; i < nums.length; ++i)
            for (int  j = target; j >= nums[i]; --j)
                f[j] += f[j - nums[i]];

        return f[target];
    }
}

  

Backpack V

标签:++i   sample   items   inline   ret   +=   ber   lin   down   

原文地址:https://www.cnblogs.com/FLAGyuri/p/12078432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!