From 00f080bc75ac4d905442acf920edc20fcd2daf6b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 28 Nov 2022 23:47:47 +0800 Subject: [PATCH 01/23] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index ad4f46d8..d1c1e1d4 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -515,6 +515,35 @@ object Solution { } ``` +## rust + +构建二叉树的有序数组: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn get_minimum_difference(root: Option>>) -> i32 { + let mut vec = vec![]; + Self::traversal(root, &mut vec); + let mut min = i32::MAX; + for i in 1..vec.len() { + min = min.min(vec[i] - vec[i - 1]) + } + min + } + pub fn traversal(root: Option>>, v: &mut Vec) { + if root.is_none() { + return; + } + let node = root.as_ref().unwrap().borrow(); + Self::traversal(node.left.clone(), v); + v.push(node.val); + Self::traversal(node.right.clone(), v); + } +} +``` +

From e60af48906349690cb23ee74cd1584674de66299 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 29 Nov 2022 01:00:18 +0800 Subject: [PATCH 02/23] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index d1c1e1d4..984fefe5 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -544,6 +544,38 @@ impl Solution { } ``` +递归中解决 + +```rust +static mut PRE: Option = None; +static mut MIN: i32 = i32::MAX; + +impl Solution { + pub fn get_minimum_difference(root: Option>>) -> i32 { + unsafe { + PRE = None; + MIN = i32::MAX; + Self::inorder(root); + MIN + } + } + pub fn inorder(root: Option>>) { + if root.is_none() { + return; + } + let node = root.as_ref().unwrap().borrow(); + Self::inorder(node.left.clone()); + unsafe { + if let Some(pre) = PRE { + MIN = (node.val - pre).min(MIN).abs(); + } + PRE = Some(node.val) + } + Self::inorder(node.right.clone()); + } +} +``` +

From f06d81c233d63a5539a37499de10bc97af87407f Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:48:10 +0800 Subject: [PATCH 03/23] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 984fefe5..8c47b633 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -576,6 +576,38 @@ impl Solution { } ``` +迭代 + +```rust +impl Solution { + pub fn get_minimum_difference(mut root: Option>>) -> i32 { + if root.is_none() { + return 0; + } + let mut stack = vec![]; + let mut pre = -1; + let mut res = i32::MAX; + while root.is_some() || !stack.is_empty() { + while let Some(node) = root { + root = node.borrow().left.clone(); + stack.push(node); + } + + let node = stack.pop().unwrap(); + + if pre >= 0 { + res = res.min(node.borrow().val - pre); + } + + pre = node.borrow().val; + + root = node.borrow().right.clone(); + } + res + } +} +``` +

From 0a8b3f12250417add77ce137150aa0a8009e04f9 Mon Sep 17 00:00:00 2001 From: wantsnowfly <2856628706@qq.com> Date: Wed, 30 Nov 2022 15:51:48 +0800 Subject: [PATCH 04/23] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E7=9A=84=E4=B8=80=E5=A4=84=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index db2d3762..72353b29 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -341,7 +341,7 @@ class Solution { if (node1.left == null && node2.left != null) { node1.left = node2.left; } - // 若node2的左节点为空,直接赋值 + // 若node1的右节点为空,直接赋值 if (node1.right == null && node2.right != null) { node1.right = node2.right; } @@ -696,3 +696,4 @@ object Solution { + From 968dcfd4ffd8d9aff0e617ed8534bb7d8d08c6f5 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:04:29 +0800 Subject: [PATCH 05/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E5=8B=95=E6=85=8B?= =?UTF-8?q?=E8=A6=8F=E5=89=87=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 62c2cbde..84d8d835 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -615,6 +615,38 @@ char * longestPalindrome(char * s){ } ``` +C#: + +動態規則: +```c# +public class Solution { + + public string LongestPalindrome(string s) { + bool[,] dp = new bool[s.Length, s.Length]; + int maxlenth = 0; + int left = 0; + int right = 0; + for(int i = s.Length-1 ; i>=0; i--){ + for(int j = i; j maxlenth){ + maxlenth = j-i+1; + left = i; + right = j; + } + } + } + return s.Substring(left, maxlenth); + } +} +``` +

From aae0de9b8a69f25b8379b36caf37459f999d2c08 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:11:47 +0800 Subject: [PATCH 06/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E9=9B=99=E6=8C=87?= =?UTF-8?q?=E9=87=9D=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 84d8d835..4d48f184 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -647,6 +647,36 @@ public class Solution { } ``` +雙指針: +```C# +public class Solution { + int maxlenth = 0; + int left = 0; + int right = 0; + + public string LongestPalindrome(string s) { + int result = 0; + for (int i = 0; i < s.Length; i++) { + extend(s, i, i, s.Length); // 以i為中心 + extend(s, i, i + 1, s.Length); // 以i和i+1為中心 + } + return s.Substring(left, maxlenth); + } + + private void extend(string s, int i, int j, int n) { + while (i >= 0 && j < n && s[i] == s[j]) { + if (j - i + 1 > maxlenth) { + left = i; + right = j; + maxlenth = j - i + 1; + } + i--; + j++; + } + } +} +``` +

From a6d407a44ac7b4132888ec9c6650c8a77b9730a7 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 3 Dec 2022 22:36:40 +0800 Subject: [PATCH 07/23] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 3c6362bf..7966e87c 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -876,6 +876,55 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn find_mode(root: Option>>) -> Vec { + let mut count = 0; + let mut max_count = 0; + let mut res = vec![]; + let mut pre = i32::MIN; + Self::search_bst(&root, &mut pre, &mut res, &mut count, &mut max_count); + res + } + pub fn search_bst( + cur: &Option>>, + mut pre: &mut i32, + res: &mut Vec, + count: &mut i32, + max_count: &mut i32, + ) { + if cur.is_none() { + return; + } + + let cur_node = cur.as_ref().unwrap().borrow(); + Self::search_bst(&cur_node.left, pre, res, count, max_count); + if *pre == i32::MIN { + *count = 1; + } else if *pre == cur_node.val { + *count += 1; + } else { + *count = 1; + }; + match count.cmp(&max_count) { + std::cmp::Ordering::Equal => res.push(cur_node.val), + std::cmp::Ordering::Greater => { + *max_count = *count; + res.clear(); + res.push(cur_node.val); + } + _ => {} + }; + *pre = cur_node.val; + Self::search_bst(&cur_node.right, pre, res, count, max_count); + } +} +``` +

From ce622d557e640b42e72aecff7cb137571bf70e41 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 4 Dec 2022 00:01:46 +0800 Subject: [PATCH 08/23] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 7966e87c..a48fde34 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -925,6 +925,44 @@ impl Solution { } ``` +迭代: + +```rust +pub fn find_mode(root: Option>>) -> Vec { + let (mut cur, mut pre) = (root, i32::MIN); + let mut res = vec![]; + let mut stack = vec![]; + let (mut count, mut max_count) = (0, 0); + while !stack.is_empty() || cur.is_some() { + while let Some(node) = cur { + cur = node.borrow().left.clone(); + stack.push(node); + } + if let Some(node) = stack.pop() { + if pre == i32::MIN { + count = 1; + } else if pre == node.borrow().val { + count += 1; + } else { + count = 1; + } + match count.cmp(&max_count) { + std::cmp::Ordering::Equal => res.push(node.borrow().val), + std::cmp::Ordering::Greater => { + max_count = count; + res.clear(); + res.push(node.borrow().val); + } + _ => {} + } + pre = node.borrow().val; + cur = node.borrow().right.clone(); + } + } + res + } +``` +

From f056b7ece6be055f1f046869ee11c37cafaadd7d Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 4 Dec 2022 16:49:13 +0800 Subject: [PATCH 09/23] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index a34e8c21..eadae35e 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -384,6 +384,34 @@ object Solution { } ``` +## rust + +```rust +impl Solution { + pub fn lowest_common_ancestor( + root: Option>>, + p: Option>>, + q: Option>>, + ) -> Option>> { + if root == p || root == q || root.is_none() { + return root; + } + let left = Self::lowest_common_ancestor( + root.as_ref().unwrap().borrow().left.clone(), + p.clone(), + q.clone(), + ); + let right = + Self::lowest_common_ancestor(root.as_ref().unwrap().borrow().right.clone(), p, q); + match (&left, &right) { + (None, Some(_)) => right, + (Some(_), Some(_)) => root, + _ => left, + } + } +} +``` +

From 8e392449be894df8cf3d6b4de1a5b629963ea790 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:04:29 +0800 Subject: [PATCH 10/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E5=8B=95=E6=85=8B?= =?UTF-8?q?=E8=A6=8F=E5=89=87=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 62c2cbde..84d8d835 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -615,6 +615,38 @@ char * longestPalindrome(char * s){ } ``` +C#: + +動態規則: +```c# +public class Solution { + + public string LongestPalindrome(string s) { + bool[,] dp = new bool[s.Length, s.Length]; + int maxlenth = 0; + int left = 0; + int right = 0; + for(int i = s.Length-1 ; i>=0; i--){ + for(int j = i; j maxlenth){ + maxlenth = j-i+1; + left = i; + right = j; + } + } + } + return s.Substring(left, maxlenth); + } +} +``` +

From d39ae16d2bca3e6e9b7b4ad0c2810ad4fdff5b81 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:11:47 +0800 Subject: [PATCH 11/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E9=9B=99=E6=8C=87?= =?UTF-8?q?=E9=87=9D=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 84d8d835..4d48f184 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -647,6 +647,36 @@ public class Solution { } ``` +雙指針: +```C# +public class Solution { + int maxlenth = 0; + int left = 0; + int right = 0; + + public string LongestPalindrome(string s) { + int result = 0; + for (int i = 0; i < s.Length; i++) { + extend(s, i, i, s.Length); // 以i為中心 + extend(s, i, i + 1, s.Length); // 以i和i+1為中心 + } + return s.Substring(left, maxlenth); + } + + private void extend(string s, int i, int j, int n) { + while (i >= 0 && j < n && s[i] == s[j]) { + if (j - i + 1 > maxlenth) { + left = i; + right = j; + maxlenth = j - i + 1; + } + i--; + j++; + } + } +} +``` +

From e75f76466952eb9091334c05bb1bf0cba8b16329 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:04:29 +0800 Subject: [PATCH 12/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E5=8B=95=E6=85=8B?= =?UTF-8?q?=E8=A6=8F=E5=89=87=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 62c2cbde..84d8d835 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -615,6 +615,38 @@ char * longestPalindrome(char * s){ } ``` +C#: + +動態規則: +```c# +public class Solution { + + public string LongestPalindrome(string s) { + bool[,] dp = new bool[s.Length, s.Length]; + int maxlenth = 0; + int left = 0; + int right = 0; + for(int i = s.Length-1 ; i>=0; i--){ + for(int j = i; j maxlenth){ + maxlenth = j-i+1; + left = i; + right = j; + } + } + } + return s.Substring(left, maxlenth); + } +} +``` +

From 4621def631c57ff5fe0c360185268fd9813b71b2 Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 30 Nov 2022 23:11:47 +0800 Subject: [PATCH 13/23] =?UTF-8?q?=E5=A2=9E=E5=8A=A00005.=E6=9C=80=E9=95=B7?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2C#=20=E9=9B=99=E6=8C=87?= =?UTF-8?q?=E9=87=9D=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 84d8d835..4d48f184 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -647,6 +647,36 @@ public class Solution { } ``` +雙指針: +```C# +public class Solution { + int maxlenth = 0; + int left = 0; + int right = 0; + + public string LongestPalindrome(string s) { + int result = 0; + for (int i = 0; i < s.Length; i++) { + extend(s, i, i, s.Length); // 以i為中心 + extend(s, i, i + 1, s.Length); // 以i和i+1為中心 + } + return s.Substring(left, maxlenth); + } + + private void extend(string s, int i, int j, int n) { + while (i >= 0 && j < n && s[i] == s[j]) { + if (j - i + 1 > maxlenth) { + left = i; + right = j; + maxlenth = j - i + 1; + } + i--; + j++; + } + } +} +``` +

From 2944b9152086d15662f4fafb636f993a9b16378c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 17 Dec 2022 14:19:09 +0800 Subject: [PATCH 14/23] =?UTF-8?q?update=200435.=E6=97=A0=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=EF=BC=9A=E6=9B=B4=E6=96=B0=20java=20?= =?UTF-8?q?=E5=92=8C=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0435.无重叠区间.md | 66 +++++++++++++++----------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index 6ac29996..33f8c3f2 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -62,9 +62,9 @@ 每次取非交叉区间的时候,都是可右边界最小的来做分割点(这样留给下一个区间的空间就越大),所以第一条分割线就是区间1结束的位置。 -接下来就是找大于区间1结束位置的区间,是从区间4开始。**那有同学问了为什么不从区间5开始?别忘已经是按照右边界排序的了**。 +接下来就是找大于区间1结束位置的区间,是从区间4开始。**那有同学问了为什么不从区间5开始?别忘了已经是按照右边界排序的了**。 -区间4结束之后,在找到区间6,所以一共记录非交叉区间的个数是三个。 +区间4结束之后,再找到区间6,所以一共记录非交叉区间的个数是三个。 总共区间个数为6,减去非交叉区间的个数3。移除区间的最小数量就是3。 @@ -183,23 +183,19 @@ public: ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { - Arrays.sort(intervals, (a, b) -> { - // 按照区间右边界升序排序 - return a[1] - b[1]; + Arrays.sort(intervals, (a,b)-> { + return Integer.compare(a[0],b[0]); }); - - int count = 0; - int edge = Integer.MIN_VALUE; - for (int i = 0; i < intervals.length; i++) { - // 若上一个区间的右边界小于当前区间的左边界,说明无交集 - if (edge <= intervals[i][0]) { - edge = intervals[i][1]; - } else { + int count = 1; + for(int i = 1;i < intervals.length;i++){ + if(intervals[i][0] < intervals[i-1][1]){ + intervals[i][1] = Math.min(intervals[i - 1][1], intervals[i][1]); + continue; + }else{ count++; - } + } } - - return count; + return intervals.length - count; } } ``` @@ -208,16 +204,15 @@ class Solution { ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { - - Arrays.sort(intervals,(a,b)->{ + Arrays.sort(intervals, (a,b)-> { return Integer.compare(a[0],b[0]); }); int remove = 0; int pre = intervals[0][1]; - for(int i=1;iintervals[i][0]) { + for(int i = 1; i < intervals.length; i++) { + if(pre > intervals[i][0]) { remove++; - pre = Math.min(pre,intervals[i][1]); + pre = Math.min(pre, intervals[i][1]); } else pre = intervals[i][1]; } @@ -242,27 +237,26 @@ class Solution: ``` ### Go -```golang +```go func eraseOverlapIntervals(intervals [][]int) int { - var flag int - //先排序 - sort.Slice(intervals,func(i,j int)bool{ - return intervals[i][0]intervals[i][0]{ - flag++ - intervals[i][1]=min(intervals[i-1][1],intervals[i][1])//由于是先排序的,所以,第一位是递增顺序,故只需要将临近两个元素的第二个值最小值更新到该元素的第二个值即可作之后的判断 + res := 1 + for i := 1; i < len(intervals); i++ { + if intervals[i][0] >= intervals[i-1][1] { + res++ + } else { + intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) } } - return flag + return len(intervals) - res } -func min(a,b int)int{ - if a>b{ - return b +func min(a, b int) int { + if a < b { + return a } - return a + return b } ``` From 992ef648f60c8f1910c5dafabfbec1f6db5dcbad Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 17 Dec 2022 14:31:46 +0800 Subject: [PATCH 15/23] =?UTF-8?q?update=200056.=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4:=20=E6=9B=BF=E6=8D=A2=20go=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=92=8Cmarkdown=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 2ff37372..7b092c9b 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -207,24 +207,26 @@ class Solution: ``` ### Go -```golang +```go func merge(intervals [][]int) [][]int { - //先从小到大排序 - sort.Slice(intervals,func(i,j int)bool{ - return intervals[i][0]=intervals[i+1][0]{ - intervals[i][1]=max(intervals[i][1],intervals[i+1][1])//赋值最大值 - intervals=append(intervals[:i+1],intervals[i+2:]...) - i-- + res := make([][]int, 0, len(intervals)) + left, right := intervals[0][0], intervals[0][1] + for i := 1; i < len(intervals); i++ { + if right < intervals[i][0] { + res = append(res, []int{left, right}) + left, right = intervals[i][0], intervals[i][1] + } else { + right = max(right, intervals[i][1]) } } - return intervals + res = append(res, []int{left, right}) // 将最后一个区间放入 + return res } -func max(a,b int)int{ - if a>b{ +func max(a, b int) int { + if a > b { return a } return b From f30fc5649df4a450406c65811d0979c9011d99fd Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 17 Dec 2022 16:02:03 +0800 Subject: [PATCH 16/23] =?UTF-8?q?update=200738.=E5=8D=95=E8=B0=83=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97:=20=E4=BF=AE=E6=94=B9=20go?= =?UTF-8?q?=20=E4=BB=A3=E7=A0=81=E5=A4=84markdown=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0738.单调递增的数字.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 89630827..e8666bcd 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -179,7 +179,7 @@ class Solution: ``` ### Go -```golang +```go func monotoneIncreasingDigits(N int) int { s := strconv.Itoa(N)//将数字转为字符串,方便使用下标 ss := []byte(s)//将字符串转为byte数组,方便更改。 @@ -187,10 +187,10 @@ func monotoneIncreasingDigits(N int) int { if n <= 1 { return N } - for i:=n-1 ; i>0; i-- { - if ss[i-1] > ss[i] {//前一个大于后一位,前一位减1,后面的全部置为9 + for i := n-1; i > 0; i-- { + if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9 ss[i-1] -= 1 - for j := i ; j < n; j++ {//后面的全部置为9 + for j := i; j < n; j++ { //后面的全部置为9 ss[j] = '9' } } From f77fa05df7f506b33be4efa4eaca07beabc2e725 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 17 Dec 2022 16:26:04 +0800 Subject: [PATCH 17/23] =?UTF-8?q?update=200714.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=9A=E4=BF=AE=E6=94=B9markdown?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14.买卖股票的最佳时机含手续费.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index faa56d42..005f9c38 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -217,25 +217,25 @@ class Solution: # 贪心思路 ``` ### Go -```golang +```go func maxProfit(prices []int, fee int) int { var minBuy int = prices[0] //第一天买入 var res int - for i:=0;i=minBuy&&prices[i]-fee-minBuy<=0{ + if prices[i] >= minBuy && prices[i]-fee-minBuy <= 0 { continue } //可以售卖了 - if prices[i]>minBuy+fee{ + if prices[i] > minBuy+fee { //累加每天的收益 - res+=prices[i]-minBuy-fee + res += prices[i]-minBuy-fee //更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!) - minBuy=prices[i]-fee + minBuy = prices[i]-fee } } return res From 723286ddf8580c91c9af35bb38aca5c8440c11da Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:22:24 +0800 Subject: [PATCH 18/23] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 8c47b633..bc9645dc 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -547,31 +547,25 @@ impl Solution { 递归中解决 ```rust -static mut PRE: Option = None; -static mut MIN: i32 = i32::MAX; - impl Solution { pub fn get_minimum_difference(root: Option>>) -> i32 { - unsafe { - PRE = None; - MIN = i32::MAX; - Self::inorder(root); - MIN - } + let mut pre = None; + let mut min = i32::MAX; + Self::inorder(root, &mut pre, &mut min); + min } - pub fn inorder(root: Option>>) { + pub fn inorder(root: Option>>, pre: &mut Option, min: &mut i32) { if root.is_none() { return; } let node = root.as_ref().unwrap().borrow(); - Self::inorder(node.left.clone()); - unsafe { - if let Some(pre) = PRE { - MIN = (node.val - pre).min(MIN).abs(); - } - PRE = Some(node.val) + Self::inorder(node.left.clone(), pre, min); + if let Some(pre) = pre { + *min = (node.val - *pre).min(*min); } - Self::inorder(node.right.clone()); + *pre = Some(node.val); + + Self::inorder(node.right.clone(), pre, min); } } ``` From 363d4e6fcbf3bceebee14053583f9cb555e6987d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 20 Dec 2022 15:20:15 +0800 Subject: [PATCH 19/23] =?UTF-8?q?update=200096.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=9A=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=96=87=E6=9C=AC=20=E5=92=8C=20go=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0096.不同的二叉搜索树.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index d086b1f3..519fd323 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -69,7 +69,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索 **dp[i] : 1到i为节点组成的二叉搜索树的个数为dp[i]**。 -也可以理解是i的不同元素节点组成的二叉搜索树的个数为dp[i] ,都是一样的。 +也可以理解是i个不同元素节点组成的二叉搜索树的个数为dp[i] ,都是一样的。 以下分析如果想不清楚,就来回想一下dp[i]的定义 @@ -199,11 +199,11 @@ class Solution: ### Go ```Go func numTrees(n int)int{ - dp:=make([]int,n+1) - dp[0]=1 - for i:=1;i<=n;i++{ - for j:=1;j<=i;j++{ - dp[i]+=dp[j-1]*dp[i-j] + dp := make([]int, n+1) + dp[0] = 1 + for i := 1; i <= n; i++ { + for j := 1; j <= i; j++ { + dp[i] += dp[j-1] * dp[i-j] } } return dp[n] From 4067790b7c74bcde83dd177ccc44687a87928218 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 20 Dec 2022 16:32:29 +0800 Subject: [PATCH 20/23] =?UTF-8?q?update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=EF=BC=9A=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E9=94=99=E5=AD=97=EF=BC=8C=E5=88=A0=E9=99=A4=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E8=AF=AD=E8=A8=80=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 100 +++------------------------- 1 file changed, 11 insertions(+), 89 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 52b48264..ecee1d83 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -32,7 +32,7 @@ ## 思路 -这道题目初步看,是如下两题几乎是一样的,大家可以用回溯法,解决如下两题 +这道题目初步看,和如下两题几乎是一样的,大家可以用回溯法,解决如下两题 * 698.划分为k个相等的子集 * 473.火柴拼正方形 @@ -62,7 +62,7 @@ 回归主题:首先,本题要求集合里能否出现总和为 sum / 2 的子集。 -那么来一一对应一下本题,看看背包问题如果来解决。 +那么来一一对应一下本题,看看背包问题如何来解决。 **只有确定了如下四点,才能把01背包问题套到本题上来。** @@ -77,9 +77,9 @@ 1. 确定dp数组以及下标的含义 -01背包中,dp[j] 表示: 容量为j的背包,所背的物品价值可以最大为dp[j]。 +01背包中,dp[j] 表示: 容量为j的背包,所背的物品价值最大可以为dp[j]。 -本题中每一个元素的数值即是重量,也是价值。 +本题中每一个元素的数值既是重量,也是价值。 **套到本题,dp[j]表示 背包总容量(所能装的总重量)是j,放进物品后,背的最大重量为dp[j]**。 @@ -106,9 +106,9 @@ 从dp[j]的定义来看,首先dp[0]一定是0。 -如果如果题目给的价值都是正整数那么非0下标都初始化为0就可以了,如果题目给的价值有负数,那么非0下标就要初始化为负无穷。 +如果题目给的价值都是正整数那么非0下标都初始化为0就可以了,如果题目给的价值有负数,那么非0下标就要初始化为负无穷。 -**这样才能让dp数组在递归公式的过程中取的最大的价值,而不是被初始值覆盖了**。 +**这样才能让dp数组在递推的过程中取得最大的价值,而不是被初始值覆盖了**。 本题题目中 只包含正整数的非空数组,所以非0下标的元素初始化为0就可以了。 @@ -202,15 +202,15 @@ class Solution { if(nums == null || nums.length == 0) return false; int n = nums.length; int sum = 0; - for(int num : nums){ + for(int num : nums) { sum += num; } //总和为奇数,不能平分 if(sum % 2 != 0) return false; int target = sum / 2; int[] dp = new int[target + 1]; - for(int i = 0; i < n; i++){ - for(int j = target; j >= nums[i]; j--){ + for(int i = 0; i < n; i++) { + for(int j = target; j >= nums[i]; j--) { //物品 i 的重量是 nums[i],其价值也是 nums[i] dp[j] = Math.max(dp[j], dp[j-nums[i]] + nums[i]); } @@ -220,6 +220,7 @@ class Solution { } ``` +二维数组版本(易于理解): ```java public class Solution { public static void main(String[] args) { @@ -288,46 +289,6 @@ false true false false false true true false false false false true false true false false false true true false false false true true ``` - -二维数组版本(易于理解): -```Java -class Solution { - public boolean canPartition(int[] nums) { - int sum = 0; - for (int i = 0; i < nums.length; i++) { - sum += nums[i]; - } - - if (sum % 2 == 1) - return false; - int target = sum / 2; - - //dp[i][j]代表可装物品为0-i,背包容量为j的情况下,背包内容量的最大价值 - int[][] dp = new int[nums.length][target + 1]; - - //初始化,dp[0][j]的最大价值nums[0](if j > weight[i]) - //dp[i][0]均为0,不用初始化 - for (int j = nums[0]; j <= target; j++) { - dp[0][j] = nums[0]; - } - - //遍历物品,遍历背包 - //递推公式: - for (int i = 1; i < nums.length; i++) { - for (int j = 0; j <= target; j++) { - //背包容量可以容纳nums[i] - if (j >= nums[i]) { - dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); - } else { - dp[i][j] = dp[i - 1][j]; - } - } - } - - return dp[nums.length - 1][target] == target; - } -} -``` ### Python: ```python class Solution: @@ -341,6 +302,7 @@ class Solution: dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) return target == dp[target] ``` + ### Go: ```go // 分割等和子集 动态规划 @@ -369,46 +331,6 @@ func canPartition(nums []int) bool { } ``` -```go -func canPartition(nums []int) bool { - /** - 动态五部曲: - 1.确定dp数组和下标含义 - 2.确定递推公式 - 3.dp数组初始化 - 4.dp遍历顺序 - 5.打印 - **/ - //确定和 - var sum int - for _,v:=range nums{ - sum+=v - } - if sum%2!=0{ //如果和为奇数,则不可能分成两个相等的数组 - return false - } - sum/=2 - //确定dp数组和下标含义 - var dp [][]bool //dp[i][j] 表示: 前i个石头是否总和不大于J - //初始化数组 - dp=make([][]bool,len(nums)+1) - for i,_:=range dp{ - dp[i]=make([]bool,sum+1) - dp[i][0]=true - } - for i:=1;i<=len(nums);i++{ - for j:=1;j<=sum;j++{//j是固定总量 - if j>=nums[i-1]{//如果容量够用则可放入背包 - dp[i][j]=dp[i-1][j]||dp[i-1][j-nums[i-1]] - }else{//如果容量不够用则不拿,维持前一个状态 - dp[i][j]=dp[i-1][j] - } - } - } - return dp[len(nums)][sum] -} -``` - ### javaScript: ```js From 02a01bb3aa5719634beecb1ee8a388e9d6055e03 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 20 Dec 2022 23:23:32 +0800 Subject: [PATCH 21/23] =?UTF-8?q?update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?:=20=E4=BF=AE=E6=94=B9=E6=96=87=E6=9C=AC=E9=94=99=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 946e52f2..6c8c28ec 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -123,7 +123,7 @@ public: x = (target + sum) / 2 -**此时问题就转化为,装满容量为x背包,有几种方法**。 +**此时问题就转化为,装满容量为x的背包,有几种方法**。 这里的x,就是bagSize,也就是我们后面要求的背包容量。 @@ -184,11 +184,11 @@ dp[j] += dp[j - nums[i]] 3. dp数组如何初始化 -从递归公式可以看出,在初始化的时候dp[0] 一定要初始化为1,因为dp[0]是在公式中一切递推结果的起源,如果dp[0]是0的话,递归结果将都是0。 +从递推公式可以看出,在初始化的时候dp[0] 一定要初始化为1,因为dp[0]是在公式中一切递推结果的起源,如果dp[0]是0的话,递推结果将都是0。 这里有录友可能认为从dp数组定义来说 dp[0] 应该是0,也有录友认为dp[0]应该是1。 -其实不要硬去解释它的含义,咱就把 dp[0]的情况带入本题看看就是应该等于多少。 +其实不要硬去解释它的含义,咱就把 dp[0]的情况带入本题看看应该等于多少。 如果数组[0] ,target = 0,那么 bagSize = (target + sum) / 2 = 0。 dp[0]也应该是1, 也就是说给数组里的元素 0 前面无论放加法还是减法,都是 1 种方法。 @@ -198,7 +198,7 @@ dp[j] += dp[j - nums[i]] 其实 此时最终的dp[0] = 32,也就是这五个零 子集的所有组合情况,但此dp[0]非彼dp[0],dp[0]能算出32,其基础是因为dp[0] = 1 累加起来的。 -dp[j]其他下标对应的数值也应该初始化为0,从递归公式也可以看出,dp[j]要保证是0的初始值,才能正确的由dp[j - nums[i]]推导出来。 +dp[j]其他下标对应的数值也应该初始化为0,从递推公式也可以看出,dp[j]要保证是0的初始值,才能正确的由dp[j - nums[i]]推导出来。 4. 确定遍历顺序 @@ -245,7 +245,7 @@ public: ## 总结 -此时 大家应该不仅想起,我们之前讲过的[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)是不是应该也可以用dp来做啊? +此时 大家应该不禁想起,我们之前讲过的[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)是不是应该也可以用dp来做啊? 是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)要求的是把所有组合列出来,还是要使用回溯法爆搜的。 From df5efcd1b1ac2eda08c4b44a17a5facd98d03c63 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Wed, 21 Dec 2022 14:00:11 +0800 Subject: [PATCH 22/23] =?UTF-8?q?update=200474.=E4=B8=80=E5=92=8C=E9=9B=B6?= =?UTF-8?q?:=20=E4=BC=98=E5=8C=96=E6=96=87=E6=9C=AC=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=86=97=E4=BD=99=E7=9A=84=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 53 +++----------------------------------- 1 file changed, 3 insertions(+), 50 deletions(-) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 45a0a270..213a2aa2 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -59,7 +59,7 @@ 但本题其实是01背包问题! -这不过这个背包有两个维度,一个是m 一个是n,而不同长度的字符串就是不同大小的待装物品。 +只不过这个背包有两个维度,一个是m 一个是n,而不同长度的字符串就是不同大小的待装物品。 开始动规五部曲: @@ -114,7 +114,7 @@ for (string str : strs) { // 遍历物品 有同学可能想,那个遍历背包容量的两层for循环先后循序有没有什么讲究? -没讲究,都是物品重量的一个维度,先遍历那个都行! +没讲究,都是物品重量的一个维度,先遍历哪个都行! 5. 举例推导dp数组 @@ -152,7 +152,7 @@ public: ## 总结 -不少同学刷过这道提,可能没有总结这究竟是什么背包。 +不少同学刷过这道题,可能没有总结这究竟是什么背包。 此时我们讲解了0-1背包的多种应用, @@ -252,53 +252,6 @@ func max(a,b int) int { return b } ``` -> 传统背包,三维数组法 -```golang -func findMaxForm(strs []string, m int, n int) int { - //dp的第一个index代表项目的多少,第二个代表的是背包的容量 - //所以本处项目的多少是len(strs),容量为m和n - dp:=make([][][]int,len(strs)+1) - for i:=0;i<=len(strs);i++{ - //初始化背包容量 - strDp:=make([][]int,m+1) - for j:=0;j=zero&&j>=one{ - dp[k][i][j]=getMax(dp[k-1][i][j],dp[k-1][i-zero][j-one]+1) - } - } - } - } - return dp[len(strs)][m][n] -} -func getMax(a,b int)int{ - if a>b{ - return a - } - return b -} -``` ### Javascript ```javascript From 36bdccd6878d0b54338741d699a4d7829318d1c1 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Wed, 21 Dec 2022 17:17:59 +0800 Subject: [PATCH 23/23] =?UTF-8?q?update=200279.=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0:=20=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99=20py?= =?UTF-8?q?thon=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 37 -------------------------------- 1 file changed, 37 deletions(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 50ddf5f9..b6a97be3 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -235,43 +235,6 @@ class Solution: return dp[n] ``` -Python3: -```python -class Solution: - '''版本一,先遍历背包, 再遍历物品''' - def numSquares(self, n: int) -> int: - dp = [n] * (n + 1) - dp[0] = 0 - # 遍历背包 - for j in range(1, n+1): - for i in range(1, n): - num = i ** 2 - if num > j: break - # 遍历物品 - if j - num >= 0: - dp[j] = min(dp[j], dp[j - num] + 1) - return dp[n] - -class Solution: - '''版本二, 先遍历物品, 再遍历背包''' - def numSquares(self, n: int) -> int: - # 初始化 - # 组成和的完全平方数的最多个数,就是只用1构成 - # 因此,dp[i] = i - dp = [i for i in range(n + 1)] - # dp[0] = 0 无意义,只是为了方便记录特殊情况: - # n本身就是完全平方数,dp[n] = min(dp[n], dp[n - n] + 1) = 1 - - for i in range(1, n): # 遍历物品 - if i * i > n: - break - num = i * i - for j in range(num, n + 1): # 遍历背包 - dp[j] = min(dp[j], dp[j - num] + 1) - - return dp[n] -``` - Go: ```go // 版本一,先遍历物品, 再遍历背包