Skip to content

Commit

Permalink
添加 0300.最长上升子序列、0322.零钱兑换、0518.零钱兑换II 和0674.最长连续递增序列的rust版本
Browse files Browse the repository at this point in the history
  • Loading branch information
wangAlpha committed May 10, 2022
1 parent 555f384 commit 8bab33d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
17 changes: 17 additions & 0 deletions problems/0300.最长上升子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ func lengthOfLIS(nums []int ) int {
}
```

Rust:
```rust
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
let mut dp = vec![1; nums.len() + 1];
let mut result = 1;
for i in 1..nums.len() {
for j in 0..i {
if nums[j] < nums[i] {
dp[i] = dp[i].max(dp[j] + 1);
}
result = result.max(dp[i]);
}
}
result
}
```

Javascript
```javascript
const lengthOfLIS = (nums) => {
Expand Down
20 changes: 19 additions & 1 deletion problems/0322.零钱兑换.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class Solution:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
return dp[amount] if dp[amount] < amount + 1 else -1
def coinChange1(self, coins: List[int], amount: int) -> int:
'''版本二'''
# 初始化
Expand Down Expand Up @@ -302,6 +302,24 @@ 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);
}
}
}
if dp[amount] == i32::MAX { -1 } else { dp[amount] }
}
```

Javascript:
```javascript
const coinChange = (coins, amount) => {
Expand Down
16 changes: 16 additions & 0 deletions problems/0518.零钱兑换II.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ func change(amount int, coins []int) int {
}
```

Rust:
```rust
pub fn change(amount: i32, coins: Vec<i32>) -> i32 {
let amount = amount as usize;
let coins = coins.iter().map(|&c|c as usize).collect::<Vec<usize>>();
let mut dp = vec![0usize; amount + 1];
dp[0] = 1;
for i in 0..coins.len() {
for j in coins[i]..=amount {
dp[j] += dp[j - coins[i]];
}
}
dp[amount] as i32
}
```

Javascript:
```javascript
const change = (amount, coins) => {
Expand Down
19 changes: 19 additions & 0 deletions problems/0674.最长连续递增序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class Solution:
return result
```


> 贪心法:
```python
class Solution:
Expand All @@ -237,6 +238,24 @@ class Solution:

Go:

Rust:
```rust
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
if nums.is_empty() {
return 0;
}
let mut result = 1;
let mut dp = vec![1; nums.len()];
for i in 1..nums.len() {
if nums[i - 1] < nums[i] {
dp[i] = dp[i - 1] + 1;
result = result.max(dp[i]);
}
}
result
}
```

Javascript:

> 动态规划:
Expand Down

0 comments on commit 8bab33d

Please sign in to comment.