首页 > 编程语言 > #yyds干货盘点# LeetCode程序员面试金典:硬币

#yyds干货盘点# LeetCode程序员面试金典:硬币

时间:2023-02-09 18:32:51浏览次数:45  
标签:yyds coins 10 int 金典 硬币 示例 coin LeetCode

题目:

硬币。给定数量不限的硬币,币值为25分、10分、5分和1分,编写代码计算n分有几种表示法。(结果可能会很大,你需要将结果模上1000000007)

示例1:

输入: n = 5

输出:2

解释: 有两种方式可以凑成总金额:

5=5

5=1+1+1+1+1

示例2:

输入: n = 10

输出:4

解释: 有四种方式可以凑成总金额:

10=10

10=5+5

10=5+1+1+1+1+1

10=1+1+1+1+1+1+1+1+1+1

代码实现:

class Solution {
static final int MOD = 1000000007;
int[] coins = {25, 10, 5, 1};

public int waysToChange(int n) {
int[] f = new int[n + 1];
f[0] = 1;
for (int c = 0; c < 4; ++c) {
int coin = coins[c];
for (int i = coin; i <= n; ++i) {
f[i] = (f[i] + f[i - coin]) % MOD;
}
}
return f[n];
}
}

标签:yyds,coins,10,int,金典,硬币,示例,coin,LeetCode
From: https://blog.51cto.com/u_13321676/6047256

相关文章

  • #yyds干货盘点# LeetCode面试题:回文数
    1.简述:给你一个整数x,如果x是一个回文整数,返回true;否则,返回false。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121是回文,而123不是。 示例1:输......
  • [leetcode每日一题]2.9
    ​​2331.计算布尔二叉树的值​​难度简单66给你一棵 完整二叉树 的根,这棵树有以下特征:叶子节点 要么值为 ​​0​​ 要么值为 ​​1​​ ,其中 ​​0​​ 表示 ......
  • #yyds干货盘点 歌谣学前端之React中渲染列表
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • #yyds干货盘点 歌谣学前端之React中虚拟dom
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • [LeetCode] 1797. Design Authentication Manager
    Thereisanauthenticationsystemthatworkswithauthenticationtokens.Foreachsession,theuserwillreceiveanewauthenticationtokenthatwillexpire t......
  • LeetCode 组合总和(/回溯+剪枝)
    原题解题目约束题解不剪枝classSolution{public:voiddfs(vector<int>&candidates,inttarget,vector<vector<int>>&ans,vector<int>&combine,......
  • 2.K个排序链表归并(Leetcode 23)
    方法一:#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};#include<vector>#include<algorithm>b......
  • 3.链表划分(Leetcode 86)
    3.链表划分(Leetcode86)#include<stdio.h> structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{public:......
  • 4.反转链表 ||(Leetcode 92)
    4.反转链表||(Leetcode92)#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{public:......
  • 5.复杂链表的深度拷贝(Leetcode 138)
    5.复杂链表的深度拷贝(Leetcode138) #include<stdio.h> structRandomListNode{ intlabel; RandomListNode*next,*random; RandomListNode(intx):label(x),......