From 4fe73f30dc793ace07e15b23ff0956eb221c5856 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:18:55 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=B9=A6=E5=86=99=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8Cnull=E5=86=99=E6=88=90=E4=BA=86nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0450.删除二叉搜索树中的节点.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 3d73598d..28c5a4c0 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == nullptr) return root; +if (root == null) return root; ``` * 确定单层递归的逻辑 From 751ba68faa3d16b45fce13e0ada8aac58d24e719 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:35:56 +0800 Subject: [PATCH 2/8] =?UTF-8?q?null=E5=86=99=E6=88=90=E4=BA=86nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 28c5a4c0..48737486 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == nullptr) return root->right; + if (root->left == null) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) return root->left; + else if (root->right == null) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == nullptr && root->right == nullptr) { + if (root->left == null && root->right == null) { ///! 内存释放 delete root; - return nullptr; + return null; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == nullptr) { + else if (root->left == null) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) { + else if (root->right == null) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; if (root->val == key) { - if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == nullptr) return target; - if (target->right == nullptr) return target->left; + if (target == null) return target; + if (target->right == null) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; TreeNode* cur = root; - TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur + TreeNode* pre = null; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == nullptr) { // 如果搜索树只有头结点 + if (pre == null) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From 873257dcd812b1d190f9decbf0fed64761d23933 Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sat, 8 Jul 2023 07:25:59 +0800 Subject: [PATCH 3/8] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix Python solution comment --- problems/0001.两数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index ca62e3ed..438fa352 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -160,7 +160,7 @@ class Solution: for index, value in enumerate(nums): if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key return [records[target- value], index] - records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key + records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中 return [] ``` (版本二)使用集合 From dba8820427c64b66add3b7ad053226e7d9943e53 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 10:43:24 +0800 Subject: [PATCH 4/8] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 749dc972..d2da3737 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -455,7 +455,27 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` +Rust: +```rust +impl Solution { + /// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标 + pub fn daily_temperatures(temperatures: Vec) -> Vec { + let mut res = vec![0; temperatures.len()]; + let mut stack = vec![]; + for (idx, &value) in temperatures.iter().enumerate() { + while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value { + // 弹出,并计算res中对应位置的值 + let i = stack.pop().unwrap(); + res[i] = (idx - i) as i32; + } + // 入栈 + stack.push(idx) + } + res + } +} +```

From 965afc9af3dc5c0fb8932937770f8395c98b7b86 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:20:21 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=8C=E9=80=82?= =?UTF-8?q?=E5=90=88=E5=9B=BE=E7=9A=84=E8=A7=A3=E6=B3=95=E6=94=BE=E5=9C=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=EF=BC=8C=E7=AC=AC=E4=BA=8C=E4=B8=AA?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=EF=BC=88=E9=9A=BE=E7=90=86=E8=A7=A3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=89=E6=94=BE=E5=9C=A8=E5=85=B6=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E7=89=88=E6=9C=AC=E7=9A=84=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 48737486..d13c48e5 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -268,6 +268,34 @@ public: ## Java +```java +// 解法1(最好理解的版本) +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` + + ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -296,33 +324,57 @@ class Solution { } } ``` +递归法 ```java -// 解法2 class Solution { public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) return root; - if (root.val == key) { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } else { - TreeNode cur = root.right; - while (cur.left != null) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - return root; + if (root == null){ + return null; + } + //寻找对应的对应的前面的节点,以及他的前一个节点 + TreeNode cur = root; + TreeNode pre = null; + while (cur != null){ + if (cur.val < key){ + pre = cur; + cur = cur.right; + } else if (cur.val > key) { + pre = cur; + cur = cur.left; + }else { + break; } } - if (root.val > key) root.left = deleteNode(root.left, key); - if (root.val < key) root.right = deleteNode(root.right, key); + if (pre == null){ + return deleteOneNode(cur); + } + if (pre.left !=null && pre.left.val == key){ + pre.left = deleteOneNode(cur); + } + if (pre.right !=null && pre.right.val == key){ + pre.right = deleteOneNode(cur); + } return root; } + + public TreeNode deleteOneNode(TreeNode node){ + if (node == null){ + return null; + } + if (node.right == null){ + return node.left; + } + TreeNode cur = node.right; + while (cur.left !=null){ + cur = cur.left; + } + cur.left = node.left; + return node.right; + } } ``` + ## Python 递归法(版本一) ```python From 0fd7b04d5c0fac9a760af96d2d5de7f5beaeb896 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:42:19 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=92=A4=E5=9B=9E=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index d13c48e5..e617cc69 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == null) return root; +if (root == nullptr) return root; ``` * 确定单层递归的逻辑 @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == null) return root->right; + if (root->left == nullptr) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) return root->left; + else if (root->right == nullptr) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == null && root->right == null) { + if (root->left == nullptr && root->right == nullptr) { ///! 内存释放 delete root; - return null; + return nullptr; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == null) { + else if (root->left == nullptr) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) { + else if (root->right == nullptr) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; if (root->val == key) { - if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == null) return target; - if (target->right == null) return target->left; + if (target == nullptr) return target; + if (target->right == nullptr) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; TreeNode* cur = root; - TreeNode* pre = null; // 记录cur的父节点,用来删除cur + TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == null) { // 如果搜索树只有头结点 + if (pre == nullptr) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From 1c7b15bbbc224e37f9341161f31a94cebe73dfb0 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 18:26:09 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Update=200496.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0496.下一个更大元素I.md | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 31c3ce43..411a47df 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -387,6 +387,32 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` +Rust + +```rust +impl Solution { + pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { + let mut ans = vec![-1; nums1.len()]; + use std::collections::HashMap; + let mut map = HashMap::new(); + for (idx, &i) in nums1.iter().enumerate() { + map.insert(i, idx); + } + let mut stack = vec![]; + for (idx, &i) in nums2.iter().enumerate() { + while !stack.is_empty() && nums2[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + if let Some(&jdx) = map.get(&nums2[pos]) { + ans[jdx] = i; + } + } + stack.push(idx); + } + ans + } +} +``` +

From a31b5865df04e5004724995e2b36610ec45d9489 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 10 Jul 2023 16:00:53 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E7=BB=84=E5=90=88=E6=96=B0=E5=A2=9Ejava?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9C=AA=E5=89=AA=E6=9E=9D=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 444e15ce..8ade6e11 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -351,7 +351,30 @@ public: ### Java: +未剪枝优化 +```java +class Solution { + List> result= new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combine(int n, int k) { + backtracking(n,k,1); + return result; + } + public void backtracking(int n,int k,int startIndex){ + if (path.size() == k){ + result.add(new ArrayList<>(path)); + return; + } + for (int i =startIndex;i<=n;i++){ + path.add(i); + backtracking(n,k,i+1); + path.removeLast(); + } + } +} +``` +剪枝优化: ```java class Solution { List> result = new ArrayList<>();