diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 145f64aa..7c1206ef 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -507,6 +507,33 @@ object Solution { } ``` +### Rust + +```rust +impl Solution { + pub fn find_max_form(strs: Vec, m: i32, n: i32) -> i32 { + let (m, n) = (m as usize, n as usize); + let mut dp = vec![vec![0; n + 1]; m + 1]; + for s in strs { + let (mut one_num, mut zero_num) = (0, 0); + for c in s.chars() { + match c { + '0' => zero_num += 1, + '1' => one_num += 1, + _ => (), + } + } + for i in (zero_num..=m).rev() { + for j in (one_num..=n).rev() { + dp[i][j] = dp[i][j].max(dp[i - zero_num][j - one_num] + 1); + } + } + } + dp[m][n] + } +} +``` +

diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 130b5666..4b21008e 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -205,6 +205,30 @@ public: ### Java +```Java +//using set, aligned with the unimproved method +class Solution { + List> result = new ArrayList<>(); + List path = new ArrayList<>(); + public List> findSubsequences(int[] nums) { + backTracking(nums, 0); + return result; + } + private void backTracking(int[] nums, int startIndex){ + if(path.size() >= 2) + result.add(new ArrayList<>(path)); + HashSet hs = new HashSet<>(); + for(int i = startIndex; i < nums.length; i++){ + if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i])) + continue; + hs.add(nums[i]); + path.add(nums[i]); + backTracking(nums, i + 1); + path.remove(path.size() - 1); + } + } +} +``` ```java class Solution { diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index a559a512..088a3d50 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -426,6 +426,43 @@ object Solution { } ``` +Rust: + +```rust +impl Solution { + // 先遍历物品 + fn complete_pack() { + let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); + let mut dp = vec![0; bag_size + 1]; + for (weight, value) in goods { + for j in weight..=bag_size { + dp[j] = dp[j].max(dp[j - weight] + value); + } + } + println!("先遍历物品:{}", dp[bag_size]); + } + + // 先遍历背包 + fn complete_pack_after() { + let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); + let mut dp = vec![0; bag_size + 1]; + for i in 0..=bag_size { + for (weight, value) in &goods { + if i >= *weight { + dp[i] = dp[i].max(dp[i - weight] + value); + } + } + } + println!("先遍历背包:{}", dp[bag_size]); + } +} + +#[test] +fn test_complete_pack() { + Solution::complete_pack(); + Solution::complete_pack_after(); +} +```