From 24d7ad68f0a906fac3c7d27e6db6d7116f89e40f Mon Sep 17 00:00:00 2001 From: wantsnowfly <2856628706@qq.com> Date: Mon, 7 Nov 2022 17:56:01 +0800 Subject: [PATCH 01/15] =?UTF-8?q?=E5=A2=9E=E5=8A=A0034=20java=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...元素的第一个和最后一个位置.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index dc6833f6..c7ff6dce 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -271,6 +271,64 @@ class Solution { } ``` +```java +// 解法三 +class Solution { + public int[] searchRange(int[] nums, int target) { + int left = searchLeft(nums,target); + int right = searchRight(nums,target); + return new int[]{left,right}; + } + public int searchLeft(int[] nums,int target){ + // 寻找元素第一次出现的地方 + int left = 0; + int right = nums.length-1; + while(left<=right){ + int mid = left+(right-left)/2; + // >= 的都要缩小 因为要找第一个元素 + if(nums[mid]>=target){ + right = mid - 1; + }else{ + left = mid + 1; + } + } + // right = left - 1 + // 如果存在答案 right是首选 + if(right>=0&&right=0&&left=0&&left=0&&right<=nums.length&&nums[right]==target){ + return right; + } + return -1; + } +} +``` + ### Python @@ -685,3 +743,4 @@ class Solution { + From 3f70c16fa6bbb059cd7e102b791d25b844bafd74 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 01:29:42 +0800 Subject: [PATCH 02/15] =?UTF-8?q?update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20about=20usin?= =?UTF-8?q?g=20vecdeque=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 45 ++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 3a4e0a31..f4f51f01 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -380,29 +380,32 @@ object Solution { Rust: ```rust -pub fn level_order(root: Option>>) -> Vec> { - let mut ans = Vec::new(); - let mut stack = Vec::new(); - if root.is_none(){ - return ans; - } - stack.push(root.unwrap()); - while stack.is_empty()!= true{ - let num = stack.len(); - let mut level = Vec::new(); - for _i in 0..num{ - let tmp = stack.remove(0); - level.push(tmp.borrow_mut().val); - if tmp.borrow_mut().left.is_some(){ - stack.push(tmp.borrow_mut().left.take().unwrap()); - } - if tmp.borrow_mut().right.is_some(){ - stack.push(tmp.borrow_mut().right.take().unwrap()); - } +use std::cell::RefCell; +use std::rc::Rc; +use std::collections::VecDeque; +impl Solution { + pub fn level_order(root: Option>>) -> Vec> { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); } - ans.push(level); + while !queue.is_empty() { + let mut temp = vec![]; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + temp.push(node.borrow().val); + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + res.push(temp); + } + res } - ans } ``` From f7730fad4b3a955f6e5d031cbf2c769bbe42a228 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 18:29:56 +0800 Subject: [PATCH 03/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 45 ++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index f4f51f01..e6e8f210 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -668,29 +668,32 @@ object Solution { Rust: ```rust -pub fn level_order(root: Option>>) -> Vec> { - let mut ans = Vec::new(); - let mut stack = Vec::new(); - if root.is_none(){ - return ans; - } - stack.push(root.unwrap()); - while stack.is_empty()!= true{ - let num = stack.len(); - let mut level = Vec::new(); - for _i in 0..num{ - let tmp = stack.remove(0); - level.push(tmp.borrow_mut().val); - if tmp.borrow_mut().left.is_some(){ - stack.push(tmp.borrow_mut().left.take().unwrap()); - } - if tmp.borrow_mut().right.is_some(){ - stack.push(tmp.borrow_mut().right.take().unwrap()); - } +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn level_order_bottom(root: Option>>) -> Vec> { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); } - ans.push(level); + while !queue.is_empty() { + let mut temp = vec![]; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + temp.push(node.borrow().val); + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + res.push(temp); + } + res.into_iter().rev().collect() } - ans } ``` From 0b060140d649cd984733e6becdc9feb560a890aa Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 18:55:36 +0800 Subject: [PATCH 04/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index e6e8f210..c9a6b476 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -941,6 +941,39 @@ object Solution { } ``` +rust: + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn right_side_view(root: Option>>) -> Vec { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + let len = queue.len(); + for i in 0..len { + let node = queue.pop_front().unwrap().unwrap(); + if i == len - 1 { + res.push(node.borrow().val); + } + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + } + res + } +} +``` + # 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) From 012081f38f78465ec6994db3cf035345384ab82b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:46:44 +0800 Subject: [PATCH 05/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index c9a6b476..6cb4b5e3 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1224,6 +1224,39 @@ object Solution { } ``` +rust: + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn average_of_levels(root: Option>>) -> Vec { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + let len = queue.len(); + let mut sum = 0; + for _ in 0..len { + let node = queue.pop_front().unwrap().unwrap(); + sum += node.borrow().val; + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + res.push((sum as f64) / len as f64); + } + res + } +} +``` + # 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) From 07d365dcc5ecf862d639eaf9d747cc5f0a9c283b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 8 Nov 2022 21:34:54 +0800 Subject: [PATCH 06/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 6cb4b5e3..e75455f3 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1758,6 +1758,38 @@ object Solution { } ``` +rust: + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn largest_values(root: Option>>) -> Vec { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + let mut max = i32::MIN; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + max = max.max(node.borrow().val); + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + res.push(max); + } + res + } +} +``` + # 116.填充每个节点的下一个右侧节点指针 [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) From ece7623da2873f1c0c0ec31e037daf7d1b38162b Mon Sep 17 00:00:00 2001 From: lihuacai Date: Wed, 9 Nov 2022 02:22:43 +0800 Subject: [PATCH 07/15] =?UTF-8?q?feat:=20update=5F63=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II=5Fpython3,=20=E5=8F=8D=E5=90=91=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=94=B9=E6=88=90=E6=AD=A3=E5=90=91=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 547eb9f8..9aa36956 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -272,24 +272,28 @@ class Solution: row = len(obstacleGrid) col = len(obstacleGrid[0]) dp = [[0 for _ in range(col)] for _ in range(row)] - - dp[0][0] = 1 if obstacleGrid[0][0] != 1 else 0 - if dp[0][0] == 0: return 0 # 如果第一个格子就是障碍,return 0 + dp[0][0] = 0 if obstacleGrid[0][0] == 1 else 1 + if dp[0][0] == 0: + return 0 # 如果第一个格子就是障碍,return 0 # 第一行 for i in range(1, col): - if obstacleGrid[0][i] != 1: - dp[0][i] = dp[0][i-1] + if obstacleGrid[0][i] == 1: + # 遇到障碍物时,直接退出循环,后面默认都是0 + break + dp[0][i] = 1 # 第一列 for i in range(1, row): - if obstacleGrid[i][0] != 1: - dp[i][0] = dp[i-1][0] - print(dp) + if obstacleGrid[i][0] == 1: + # 遇到障碍物时,直接退出循环,后面默认都是0 + break + dp[i][0] = 1 + # print(dp) for i in range(1, row): for j in range(1, col): - if obstacleGrid[i][j] != 1: - dp[i][j] = dp[i-1][j] + dp[i][j-1] + if obstacleGrid[i][j] == 0: + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[-1][-1] ``` From 30550b06af1810bf6bab063cce1b3b31da0e2396 Mon Sep 17 00:00:00 2001 From: Jijie LIU Date: Tue, 8 Nov 2022 21:50:00 +0100 Subject: [PATCH 08/15] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 66 +++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 30ca3d77..37adfd54 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -754,23 +754,77 @@ func isSymmetric3(_ root: TreeNode?) -> Bool { ## Scala -递归: +> 递归: ```scala -object Solution { +object Solution { def isSymmetric(root: TreeNode): Boolean = { if (root == null) return true // 如果等于空直接返回true + def compare(left: TreeNode, right: TreeNode): Boolean = { - if (left == null && right == null) return true // 如果左右都为空,则为true - if (left == null && right != null) return false // 如果左空右不空,不对称,返回false - if (left != null && right == null) return false // 如果左不空右空,不对称,返回false + if (left == null && right == null) true // 如果左右都为空,则为true + else if (left == null && right != null) false // 如果左空右不空,不对称,返回false + else if (left != null && right == null) false // 如果左不空右空,不对称,返回false // 如果左右的值相等,并且往下递归 - left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left) + else left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left) } + // 分别比较左子树和右子树 compare(root.left, root.right) } } ``` +> 迭代 - 使用栈 +```scala +object Solution { + + import scala.collection.mutable + + def isSymmetric(root: TreeNode): Boolean = { + if (root == null) return true + + val cache = mutable.Stack[(TreeNode, TreeNode)]((root.left, root.right)) + + while (cache.nonEmpty) { + cache.pop() match { + case (null, null) => + case (_, null) => return false + case (null, _) => return false + case (left, right) => + if (left.value != right.value) return false + cache.push((left.left, right.right)) + cache.push((left.right, right.left)) + } + } + true + } +} +``` +> 迭代 - 使用队列 +```scala +object Solution { + + import scala.collection.mutable + + def isSymmetric(root: TreeNode): Boolean = { + if (root == null) return true + + val cache = mutable.Queue[(TreeNode, TreeNode)]((root.left, root.right)) + + while (cache.nonEmpty) { + cache.dequeue() match { + case (null, null) => + case (_, null) => return false + case (null, _) => return false + case (left, right) => + if (left.value != right.value) return false + cache.enqueue((left.left, right.right)) + cache.enqueue((left.right, right.left)) + } + } + true + } +} +```

From ef5b014228c2f0002637b4af0a279596a2b53a60 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Wed, 9 Nov 2022 23:19:00 +0800 Subject: [PATCH 09/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index e75455f3..f1ec3918 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2576,6 +2576,36 @@ object Solution { } ``` +rust: + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn max_depth(root: Option>>) -> i32 { + let mut queue = VecDeque::new(); + let mut res = 0; + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + res += 1; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + } + res + } +} +``` + # 111.二叉树的最小深度 [力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) From 0797680df421740987c05ca5f5d3501794b53a31 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Wed, 9 Nov 2022 23:30:55 +0800 Subject: [PATCH 10/15] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index f1ec3918..acd91c6b 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2850,6 +2850,39 @@ object Solution { } ``` +rust: + +```rust +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn min_depth(root: Option>>) -> i32 { + let mut res = 0; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + res += 1; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + if node.borrow().left.is_none() && node.borrow().right.is_none() { + return res; + } + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); + } + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); + } + } + } + res + } +} +``` + # 总结 二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。 From 75103a887219ca10080a5cdb8ef4639bfe46422c Mon Sep 17 00:00:00 2001 From: xu <44153643+xu-kai-xu@users.noreply.github.com> Date: Thu, 10 Nov 2022 19:29:01 +0800 Subject: [PATCH 11/15] =?UTF-8?q?Update=20=E9=93=BE=E8=A1=A8=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 链表理论基础,一处可能的文字错误。 --- problems/链表理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index cdd861fd..a0b13f89 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -9,7 +9,7 @@ 什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。 -链接的入口节点称为链表的头结点也就是head。 +链表的入口节点称为链表的头结点也就是head。 如图所示: ![链表1](https://img-blog.csdnimg.cn/20200806194529815.png) From a2e84d93d9103298e7c11c4042ee1352da71877b Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Thu, 10 Nov 2022 22:05:42 +0800 Subject: [PATCH 12/15] =?UTF-8?q?leetcode=20=E8=BF=98=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E8=87=AA=E5=B7=B1=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index acd91c6b..1a01c0ae 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1528,6 +1528,54 @@ object Solution { } ``` +rust: + +```rust +pub struct Solution; +#[derive(Debug, PartialEq, Eq)] +pub struct Node { + pub val: i32, + pub children: Vec>>>, +} + +impl Node { + #[inline] + pub fn new(val: i32) -> Node { + Node { + val, + children: vec![], + } + } +} + +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn level_order(root: Option>>) -> Vec> { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + let mut temp = vec![]; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + temp.push(node.borrow().val); + if !node.borrow().children.is_empty() { + for n in node.borrow().children.clone() { + queue.push_back(n); + } + } + } + res.push(temp) + } + res + } +} +``` + # 515.在每个树行中找最大值 [力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) From 27f8efa24c9d43b911fc7c05a7182508d50365cd Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 21 Nov 2022 23:12:33 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E5=9C=A8=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84C++=E4=BB=A3=E7=A0=81=E9=83=A8=E5=88=86?= =?UTF-8?q?=EF=BC=8CaddAtIndex=E6=96=B9=E6=B3=95=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E9=92=88=E5=AF=B9index=20<=200=20=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E7=BA=A0=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 1264983b..9461e263 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -108,9 +108,12 @@ public: // 如果index大于链表的长度,则返回空 // 如果index小于0,则置为0,作为链表的新头节点。 void addAtIndex(int index, int val) { - if (index > _size || index < 0) { + if (index > _size) { return; } + if (index < 0) { + index = 0; + } LinkedNode* newNode = new LinkedNode(val); LinkedNode* cur = _dummyHead; while(index--) { From cbe3bcf50a8633b2c07ff92b86b7bd9780d62848 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 22 Nov 2022 10:13:52 +0800 Subject: [PATCH 14/15] =?UTF-8?q?update=20027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0=20python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e1de7243..a563a13f 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -199,21 +199,15 @@ Python: ```python3 class Solution: def removeElement(self, nums: List[int], val: int) -> int: - if nums is None or len(nums)==0: - return 0 - l=0 - r=len(nums)-1 - while l Date: Tue, 22 Nov 2022 22:27:35 +0800 Subject: [PATCH 15/15] =?UTF-8?q?update=200209.=E9=95=BF=E5=BA=A6=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84=20python,=20js=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 66 ++++++++--------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index c912c259..220630f2 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -169,39 +169,18 @@ Python: ```python class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: - # 定义一个无限大的数 - res = float("inf") - Sum = 0 - index = 0 - for i in range(len(nums)): - Sum += nums[i] + res = float("inf") # 定义一个无限大的数 + Sum = 0 # 滑动窗口数值之和 + i = 0 # 滑动窗口起始位置 + for j in range(len(nums)): + Sum += nums[j] while Sum >= s: - res = min(res, i-index+1) - Sum -= nums[index] - index += 1 - return 0 if res==float("inf") else res -``` -```python -# 滑动窗口 -class Solution: - def minSubArrayLen(self, target: int, nums: List[int]) -> int: - if nums is None or len(nums) == 0: - return 0 - lenf = len(nums) + 1 - total = 0 - i = j = 0 - while (j < len(nums)): - total = total + nums[j] - j += 1 - while (total >= target): - lenf = min(lenf, j - i) - total = total - nums[i] + res = min(res, j-i+1) + Sum -= nums[i] i += 1 - if lenf == len(nums) + 1: - return 0 - else: - return lenf + return 0 if res == float("inf") else res ``` + Go: ```go func minSubArrayLen(target int, nums []int) int { @@ -232,22 +211,23 @@ func minSubArrayLen(target int, nums []int) int { JavaScript: ```js - var minSubArrayLen = function(target, nums) { - // 长度计算一次 - const len = nums.length; - let l = r = sum = 0, - res = len + 1; // 子数组最大不会超过自身 - while(r < len) { - sum += nums[r++]; - // 窗口滑动 - while(sum >= target) { - // r始终为开区间 [l, r) - res = res < r - l ? res : r - l; - sum-=nums[l++]; + let start, end + start = end = 0 + let sum = 0 + let len = nums.length + let ans = Infinity + + while(end < len){ + sum += nums[end]; + while (sum >= target) { + ans = Math.min(ans, end - start + 1); + sum -= nums[start]; + start++; } + end++; } - return res > len ? 0 : res; + return ans === Infinity ? 0 : ans }; ```