当前位置:网站首页>力扣:518. 零钱兑换 II
力扣:518. 零钱兑换 II
2022-08-09 22:11:00 【empty__barrel】
力扣:518. 零钱兑换 II
题目:
给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。
请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。
假设每一种面额的硬币有无限个。
题目数据保证结果符合 32 位带符号整数。
dp数组含义:
dp[j]:凑成总金额j的货币组合数为dp[j]
递推公式:
dp[j] += dp[j - coins[i]];
初始化:
- 首先dp[0] = 1从dp[i]的含义上来讲就是,凑成总金额0的货币组合数为1。
- 下标非0的dp[j]初始化为0,这样累计加dp[j - coins[i]]的时候才不会影响真正的dp[j]
遍历顺序:
完全背包问题同时又是组合,所以是从小到大遍历,是首先for物品然后嵌套for背包容量。
代码:
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount+1,0);
dp[0] = 1;
int bagweight = amount;
for(int i = 0; i < coins.size(); ++i){
for(int j = coins[i]; j <= bagweight; ++j){
dp[j] += dp[j-coins[i]];
}
}
return dp[amount];
}
};
边栏推荐
猜你喜欢
随机推荐
leetcode:332. 重新安排行程
【对象——对象及原型链上的属性——对象的操作方法】
守护进程
CV复习:softmax代码实现
DXF笔记:文字对齐的研究
Day 12 of learning to program
APS系统能消除造成生产和运输延迟的瓶颈
高数_复习_第4章:向量代数和空间解析几何
Qt message mechanism and events
leetcode:323. 无向图中连通分量的数目
[WeChat applet development (8)] Summary of audio background music playback problems
Core Data浅谈系列之五 : 在UITableView中展示
HUAWEI CLOUD escorts the whole process of "Wandering Ark" for the first time, creating a popular brand
Leetcode 530. 二叉搜索树的最小绝对差
The 2022-8-9 sixth group of input and output streams
34. Fabric2.2 证书目录里各文件作用
p5.js实现的炫酷星体旋转动画
杭电多校-Counting Stickmen-(思维+组合数+容斥)
Forbidden (CSRF token missing or incorrect.): /
OSG笔记:使用setFontResolution设置字体分辨率









