Skip to content

Commit

Permalink
feat: add Python Code
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 committed Feb 27, 2020
1 parent c7185dc commit 26c535e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions problems/518.coin-change-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ return dp[dp.length - 1];

## 代码


代码支持:Python3,JavaScript:


JavaSCript Code:

```js
/*
* @lc app=leetcode id=518 lang=javascript
Expand Down Expand Up @@ -146,10 +152,46 @@ var change = function(amount, coins) {
};
```

Python Code:

```
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [0] * (amount + 1)
dp[0] = 1
for j in range(len(coins)):
for i in range(1, amount + 1):
if i >= coins[j]:
dp[i] += dp[i - coins[j]]
return dp[-1]
```

## 扩展

这是一道很简单描述的题目, 因此很多时候会被用到大公司的电面中。

相似问题:

[322.coin-change](./322.coin-change.md)

## 附录

Python 二维解法(不推荐):

```python
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [[0 for _ in range(len(coins) + 1)] for _ in range(amount + 1)]
for j in range(len(coins) + 1):
dp[0][j] = 1

for i in range(amount + 1):
for j in range(1, len(coins) + 1):
if i >= coins[j - 1]:
dp[i][j] = dp[i - coins[j - 1]][j] + dp[i][j - 1]
else:
dp[i][j] = dp[i][j - 1]
return dp[-1][-1]
```

0 comments on commit 26c535e

Please sign in to comment.