动态规划里例题,硬币问题。

p[i] = dp[i - coin[j]] + 1;

注意i < coin[j] dp[i-coin[j]]无解都要跳过。

public class Solution
{
public int coinChange(int[] coins, int amount)
{
int[] dp = new int[amount+1];
Arrays.fill(dp,Integer.MAX_VALUE);
//dp[i] = dp[i - coin[j]] + 1;
dp[0] = 0;
for(int i = 0; i <= amount; i++)
{
for(int j = 0; j < coins.length;j++)
{
if(i < coins[j] || dp[i-coins[j]] == Integer.MAX_VALUE) continue; dp[i] = Math.min(dp[i],dp[i - coins[j]]+1);
}
} if(dp[amount] == Integer.MAX_VALUE) return -1;
else return dp[amount];
}
}

第一次看到动态规划觉得好神奇。。

322. Coin Change的更多相关文章

  1. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  2. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  4. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  5. [LC] 322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  6. dp:322. Coin Change 自下而上的dp

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 322. Coin Change零钱兑换

    网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...

  9. 322. Coin Change选取最少的硬币凑整-背包问题变形

    [抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...

随机推荐

  1. 这 30 类 CSS 选择器,你必须理解!

    CSS 选择器是一种模式,用于选择需要添加样式的元素.平时使用最多也是最简单的就是 #id..class 和标签选择器,在 CSS 中还有很多更加强大更加灵活的选择方式,尤其是在 CSS3 中,增加了 ...

  2. jquery的return this.each()的作用

    经常看到在运用jquery插件绑定事件时候,都会用到each. 下面来比较下使用return this和return this.each()在使用的区别. 注意:使用each的时候引用this,必须使 ...

  3. 如何让ubuntu启动时打印字符信息----字符启动

    一.概述 要想实现字符启动,需要修改grub.cfg(启动配置文件),将“静态启动”改为“字符启动”. 但是grub.cfg通常只作为只读文件,修改它时实际上修改的是其他的文件然后再通过update- ...

  4. STM32固件库

    一.STM32固件库开发和传统寄存器开发方式的区别 二.CMSIS标准 CMSIS标准--Cortex Microcontroller Software Interface Standard,是ARM ...

  5. 在ubuntu12.0.4上搭建samba服务器以实现文件共享

    在安装之前samba服务器之前,先进行以下配置和测试. <壹> 准备工作 一.NAT联网方式 (1)硬件连接 无需网线,无需路由器 (2)虚拟机选择NAT连接方式 (3)测试网络通不通 在 ...

  6. 快速搭建Web服务器软件PHP+Apache+MySQL

    搭建网站或者博客,需要一个合适的 Web 服务器.除了如下能在购买的虚拟空间上进行操作外,我们也可以在自己的电脑上搞定,因为可以用来方便快捷地测试网站或者博客主题,无论是 Wordpress.Joom ...

  7. Android Studio的一些技巧和使用注意事项(持续更新)

    1.创建一个项目之后默认是没有assets目录的,可以手动在main目录下创建一个assets目录. 2.

  8. C盘瘦身,可以让你的电脑C盘恢复到刚安装时的大小

    @echo offecho 正在清理系统垃圾文件,请稍等......del /f /s /q %systemdrive%\*.tmpdel /f /s /q %systemdrive%\*._mpde ...

  9. 启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory

    E:\redis>redis-server.exe redis.windows.conf [8564] 10 Oct 20:00:36.745 # Creating Server TCP lis ...

  10. oracle查询转换_inlist转换

    oracle的optimizer会对一些sql语句进行查询转换,比如: 合并视图 子查询非嵌套化 inlist转换 下面讲讲遇到的in list转化优化的案例: create table test( ...