This commit is contained in:
2026-07-23 16:56:50 +08:00
parent 124ad9afdf
commit 92784ffb0f
36 changed files with 2015 additions and 608 deletions
+52
View File
@@ -0,0 +1,52 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/coin-change/?envType=study-plan-v2&envId=top-100-liked
// 322. 零钱兑换
//
// 给你一个整数数组 `coins` ,表示不同面额的硬币;以及一个整数 `amount` ,表示总金额。
// 计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 `-1` 。
// 你可以认为每种硬币的数量是无限的。
// 示例 1
// 输入:coins = `[1, 2, 5]`, amount = `11`
// 输出:`3`
// 解释:11 = 5 + 5 + 1
// 示例 2
// 输入:coins = `[2]`, amount = `3`
// 输出:-1
// 示例 3
// 输入:coins = [1], amount = 0
// 输出:0
//
// 提示:
// - `1 <= coins.length <= 12`
// - `1 <= coins[i] <= 2^31 - 1`
// - `0 <= amount <= 10^4`
//
func coinChange(coins []int, amount int) int {
}
func TestCoinChange(t *testing.T) {
cases := []struct {
name string
coins []int
amount int
want int
}{
{"example1", []int{1, 2, 5}, 11, 3},
{"example2", []int{2}, 3, -1},
{"example3", []int{1}, 0, 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := coinChange(c.coins, c.amount)
if got != c.want {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}