mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1292 from xiaofei-2020/dp23
添加(0322.零钱兑换.md):增加typescript版本
This commit is contained in:
@ -340,7 +340,21 @@ const coinChange = (coins, amount) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function coinChange(coins: number[], amount: number): number {
|
||||
const dp: number[] = new Array(amount + 1).fill(Infinity);
|
||||
dp[0] = 0;
|
||||
for (let i = 0; i < coins.length; i++) {
|
||||
for (let j = coins[i]; j <= amount; j++) {
|
||||
if (dp[j - coins[i]] === Infinity) continue;
|
||||
dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
|
||||
}
|
||||
}
|
||||
return dp[amount] === Infinity ? -1 : dp[amount];
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user