当前位置:网站首页>零钱兑换II——【LeetCode】
零钱兑换II——【LeetCode】
2022-04-23 11:19:00 【D&Blogsphere_.】

class Solution {
//举例amount = 5, coins = [1, 2, 5]
public int change(int amount, int[] coins) {
//递推表达式
int[] dp = new int[amount + 1];
//初始化dp数组,表示金额为0时只有一种情况,也就是什么都不装
dp[0] = 1;
for(int i = 0;i < coins.length; i++) {
//先遍历coins
//dp[j]:凑成总金额j的货币组合数为dp[j]
//第一轮外层for循环,表示考虑coins[0]==1
//凑成总金额1的货币组合数为dp[1] = dp[1] + dp[0]; (就是考虑1和不考虑1的情况之和)此时可能会想dp[1]值是什么,其实dp数组初始化的时候,值全为0了,所以这里dp[1] = dp[1] + dp[0] = 0 + 1 = 1
//当j++,后dp[2] = dp[2] + dp[2-1];(就是考虑2和不考虑2的情况之和)
//为什么这里要初始化j=coins[i]??
//考虑j=coins[1]的情况:此时j==2,则dp[2] = dp[2] + dp[2-2];dp[3] = dp[3] + dp[3-2];
//注意:此时可能会想到dp[3-2]值是多少?是0吗?不是,其实dp数组在上一轮外层for循环的时候就已经对dp数组赋予了新的值了,第二轮外层for循环只是在上一轮的基础之上改变值,dp数组就是个动态变化的数组
for(int j = coins[i]; j <= amount; j++) {
dp[j] += dp[j - coins[i]];
}
// //打印dp数组
// for(int i1 = 0; i1 < dp.length;i1++) {
// System.out.print(dp[i1]);
// }
// System.out.println();
}
return dp[amount];
}
}
版权声明
本文为[D&Blogsphere_.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_46423830/article/details/124355248
边栏推荐
猜你喜欢

CUMCM 2021-B:乙醇偶合制備C4烯烴(2)

系统编程之高级文件IO(十三)——IO多路复用-select

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题

Write console script by laravel

赛微微电科创板上市破发:跌幅达26% 公司市值44亿

MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了

Cygwin 中的 rename 用法

redis优化系列(二)Redis主从原理、主从常用配置

Microsoft Access database using PHP PDO ODBC sample

Mysql8.0安装指南
随机推荐
MySQL索引优化之分页探索详细介绍
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
laravel-admin时间范围选择器dateRange默认值问题
Mysql database transaction example tutorial
学习 Go 语言 0x06:《Go 语言之旅》中 斐波纳契闭包 练习题代码
微型机器人的认知和研发技术
Upgrade the functions available for cpolar intranet penetration
GPU, CUDA,cuDNN三者的關系總結
Mba-day5 Mathematics - application problems - engineering problems
SVN的使用:
Write console script by laravel
MySQL sorting feature details
卷积层和池化层总结
Alarm scene recognition
Mysql系列SQL查询语句书写顺序及执行顺序详解
Solve the problem of "suncertpathbuilderexception: unable to find valid certification path to requested target"
MySQL数据库事务transaction示例讲解教程
MySQL interview questions explain how to set hash index
Analysis on the characteristics of the official game economic model launched by platoffarm
QT信号量 无法解析的错误的总结