diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index dfdd51257c..f53d19a1ce 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -168,6 +168,23 @@ func lengthOfLIS(nums []int ) int { } ``` +Rust: +```rust +pub fn length_of_lis(nums: Vec) -> 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) => { diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" index 3a8d0662ab..43c735be47 100644 --- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" +++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" @@ -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: '''版本二''' # 初始化 @@ -302,6 +302,24 @@ func min(a, b int) int { ``` +Rust: + +```rust +pub fn coin_change(coins: Vec, 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) => { diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index e72c5f8515..0e4a398719 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -242,6 +242,22 @@ func change(amount int, coins []int) int { } ``` +Rust: +```rust +pub fn change(amount: i32, coins: Vec) -> i32 { + let amount = amount as usize; + let coins = coins.iter().map(|&c|c as usize).collect::>(); + 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) => { diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index e941d24263..3647149009 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -218,6 +218,7 @@ class Solution: return result ``` + > 贪心法: ```python class Solution: @@ -237,6 +238,24 @@ class Solution: Go: +Rust: +```rust +pub fn find_length_of_lcis(nums: Vec) -> 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: > 动态规划: