当前位置:网站首页>力扣: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];
}
};
边栏推荐
猜你喜欢
用PLSQL导出Oracle一个表
数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
iNFTnews | 迪士尼如何布局Web3
如何知道电脑开机记录?
“我“是一名测试/开发程序员,小孙的内心独白......
Vmware中安装win7虚拟机以及相关简单知识
Sun Zhengyi lost 150 billion: it was expensive at the beginning
OSS文件上传
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
干涉BGP的选路---社团属性
随机推荐
Sun Zhengyi lost 150 billion: it was expensive at the beginning
shell数组
制定量化交易策略的基本步骤有哪些?
33. Fabric通道、组织、节点、权限间关系
xlrd 与 xlsxwritter 的基本操作
JuiceFS 在多云存储架构中的应用 | 深势科技分享
联盟链技术应用的难点
新增一地公布2022下半年软考报考时间
【TS技术课堂】时间序列预测
matplotlib散点图自定义坐标轴(文字坐标轴)
Install win7 virtual machine in Vmware and related simple knowledge
The 2022-8-9 sixth group of input and output streams
金仓数据库 KingbaseGIS 使用手册(6.6. 几何对象校验函数、6.7. 空间参考系函数)
JS--hashchange事件--使用/教程
daemon
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
迁移学习 & 凯明初始化
月薪5K的运维小白如何成为月薪5W的高级架构师?
UNI-APP_监听页面滚动h5监听页面滚动
Chapter 15 HMM模型