Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2108 from fwqaaq/patch-28
Browse files Browse the repository at this point in the history
Update 0322.零钱兑换.md about rust
  • Loading branch information
youngyangyang04 authored Jun 14, 2023
2 parents 882463b + 88f03c4 commit ff982b0
Showing 1 changed file with 73 additions and 10 deletions.
83 changes: 73 additions & 10 deletions problems/0322.零钱兑换.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,23 +355,52 @@ func min(a, b int) int {
Rust:

```rust
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let amount = amount as usize;
let mut dp = vec![i32::MAX; amount + 1];
dp[0] = 0;
for i in 0..coins.len() {
for j in coins[i] as usize..=amount {
if dp[j - coins[i] as usize] != i32::MAX {
dp[j] = dp[j].min(dp[j - coins[i] as usize] + 1);
// 遍历物品
impl Solution {
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let amount = amount as usize;
let mut dp = vec![i32::MAX; amount + 1];
dp[0] = 0;
for coin in coins {
for i in coin as usize..=amount {
if dp[i - coin as usize] != i32::MAX {
dp[i] = dp[i].min(dp[i - coin as usize] + 1);
}
}
}
if dp[amount] == i32::MAX {
return -1;
}
dp[amount]
}
if dp[amount] == i32::MAX { -1 } else { dp[amount] }
}
```

```rust
// 遍历背包
impl Solution {
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let amount = amount as usize;
let mut dp = vec![i32::MAX; amount + 1];
dp[0] = 0;
for i in 1..=amount {
for &coin in &coins {
if i >= coin as usize && dp[i - coin as usize] != i32::MAX {
dp[i] = dp[i].min(dp[i - coin as usize] + 1)
}
}
}
if dp[amount] == i32::MAX {
return -1;
}
dp[amount]
}
}
```

Javascript:
```javascript
// 遍历物品
const coinChange = (coins, amount) => {
if(!amount) {
return 0;
Expand All @@ -380,7 +409,7 @@ const coinChange = (coins, amount) => {
let dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;

for(let i =0; i < coins.length; i++) {
for(let i = 0; i < coins.length; i++) {
for(let j = coins[i]; j <= amount; j++) {
dp[j] = Math.min(dp[j - coins[i]] + 1, dp[j]);
}
Expand All @@ -390,9 +419,26 @@ const coinChange = (coins, amount) => {
}
```

```javascript
// 遍历背包
var coinChange = function(coins, amount) {
const dp = Array(amount + 1).fill(Infinity)
dp[0] = 0
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i >= coins[j] && dp[i - coins[j]] !== Infinity) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1)
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
}
```

TypeScript:

```typescript
// 遍历物品
function coinChange(coins: number[], amount: number): number {
const dp: number[] = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
Expand All @@ -406,6 +452,23 @@ function coinChange(coins: number[], amount: number): number {
};
```

```typescript
// 遍历背包
function coinChange(coins: number[], amount: number): number {
const dp: number[] = Array(amount + 1).fill(Infinity)
dp[0] = 0
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i >= coins[j] && dp[i - coins[j]] !== Infinity) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1)
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down

0 comments on commit ff982b0

Please sign in to comment.