Lintcode(M)backpackVI

本文探讨了如何使用动态规划解决背包问题中的组合计数问题。通过一个具体例子,展示了如何编写代码来找出所有可能的组合方式,并给出了初始代码及存在的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Backpack VI

描述
笔记
数据
评测
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.

您在真实的面试中是否遇到过这个题? Yes
样例
Given nums = [1, 2, 4], target = 4

The possible combination ways are:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]
return 6

标签
动态规划
相关题目

思路:动态规划

 int  dp[target+1] ;dp[0]=1;
        for(int i=1;i<target+1;++i){
          for(auto a:nums){
              if(i>=a)
              dp[i]+=dp[i-a];
          }

        }
        return dp[target];

这是最初代码,思路是正确,但是一直报错,同一组数据,多次测试,答案居然不断翻倍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值