From 898c42a990b57ab9824d53efd7a2b647dd2724d9 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Mon, 8 May 2023 18:10:28 -0400 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E7=94=A8set2k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 436dbf01..2a58f550 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -203,6 +203,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 { From 41537211f86035ac58a7a0121e35d4838ebd61f2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 16:07:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C=E9=9B=B6.m?= =?UTF-8?q?d=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 6a178a25..145f6ec1 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -491,6 +491,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] + } +} +``` +

From 8b6e90d2e24bdec8574a213f16f81c049f7e5fb2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 17:36:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础完全背包.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index e927aa20..9a48cb71 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -388,6 +388,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(); +} +```