From 83096415a05cd5c6b1cb6a0269ca8327e993148a Mon Sep 17 00:00:00 2001 From: Galaxies2580 Date: Mon, 26 Sep 2022 09:14:58 +0800 Subject: [PATCH 1/8] add 0055 go tanxin --- problems/0055.跳跃游戏.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 394117ee..f2bcedcc 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -139,7 +139,31 @@ func canJUmp(nums []int) bool { } ``` +```go +// 贪心 +func canJump(nums []int) bool { + cover := 0 + n := len(nums)-1 + for i := 0; i <= cover; i++ { // 每次与覆盖值比较 + cover = max(i+nums[i], cover) //每走一步都将 cover 更新为最大值 + if cover >= n { + return true + } + } + return false +} +func max(a, b int ) int { + if a > b { + return a + } + return b +} +``` + + + ### Javascript + ```Javascript var canJump = function(nums) { if(nums.length === 1) return true From 37d518f77632a920bb1dbf84a9ab34472437c431 Mon Sep 17 00:00:00 2001 From: Galaxies2580 Date: Mon, 26 Sep 2022 09:23:45 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0Go=E7=89=88=E6=9C=AC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index dc037f0a..eecc4dc1 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -437,6 +437,33 @@ func countNodes(root *TreeNode) int { } ``` +迭代法 + +```go +func countNodes(root *TreeNode) int { + if root == nil { + return 0 + } + q := list.New() + q.PushBack(root) + res := 0 + for q.Len() > 0 { + n := q.Len() + for i := 0; i < n; i++ { + node := q.Remove(q.Front()).(*TreeNode) + if node.Left != nil { + q.PushBack(node.Left) + } + if node.Right != nil { + q.PushBack(node.Right) + } + res++ + } + } + return res +} +``` + ## JavaScript: From f109f92a31bd231772f17a56432ca9f0589a0bf8 Mon Sep 17 00:00:00 2001 From: Galaxies2580 Date: Mon, 26 Sep 2022 09:30:14 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0559.n=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6Go=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 2bcc9c5c..b74529d6 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -552,6 +552,29 @@ func maxdepth(root *treenode) int { ``` +### 559. n叉树的最大深度 + +```go +func maxDepth(root *Node) int { + if root == nil { + return 0 + } + q := list.New() + q.PushBack(root) + depth := 0 + for q.Len() > 0 { + n := q.Len() + for i := 0; i < n; i++ { + node := q.Remove(q.Front()).(*Node) + for j := range node.Children { + q.PushBack(node.Children[j]) + } + } + depth++ + } + return depth +} +``` ## javascript From 626f985e0ac033de68b1fad8e7453f8f624e39d2 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 26 Sep 2022 16:18:11 +0800 Subject: [PATCH 4/8] =?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=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/链表理论基础.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 8378f7f2..455a2bfe 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -218,6 +218,21 @@ class ListNode(_x: Int = 0, _next: ListNode = null) { } ``` +Rust: +```rust +pub struct Node { + value: T, + next: Link, +} + +type Link = Option>>; + +// 附设头节点 +pub struct List { + head: Link, +} +``` + -----------------------
From 9b7cf50c5a67596c50b9f720d1c845bff83ebc49 Mon Sep 17 00:00:00 2001 From: Galaxies2580 Date: Tue, 27 Sep 2022 09:29:44 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A00045=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8F=E7=9A=84go=E8=B4=AA=E5=BF=83=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=92=8Cpython=E8=B4=AA=E5=BF=83=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 13142c99..59e30df5 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -214,7 +214,26 @@ class Solution: return ans ``` +```python +# 贪心版本二 +class Solution: + def jump(self, nums: List[int]) -> int: + if len(nums) == 1: + return 0 + curDistance, nextDistance = 0, 0 + step = 0 + for i in range(len(nums)-1): + nextDistance = max(nextDistance, nums[i]+i) + if i == curDistance: + curDistance = nextDistance + step += 1 + return step +``` + + + ### Go + ```Go func jump(nums []int) int { dp := make([]int, len(nums)) @@ -240,7 +259,71 @@ func min(a, b int) int { } ``` +```go +// 贪心版本一 +func jump(nums []int) int { + n := len(nums) + if n == 1 { + return 0 + } + cur, next := 0, 0 + step := 0 + for i := 0; i < n; i++ { + next = max(nums[i]+i, next) + if i == cur { + if cur != n-1 { + step++ + cur = next + if cur >= n-1 { + return step + } + } else { + return step + } + } + } + return step +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + +```go +// 贪心版本二 +func jump(nums []int) int { + n := len(nums) + if n == 1 { + return 0 + } + cur, next := 0, 0 + step := 0 + for i := 0; i < n-1; i++ { + next = max(nums[i]+i, next) + if i == cur { + cur = next + step++ + } + } + return step +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + ### Javascript + ```Javascript var jump = function(nums) { let curIndex = 0 From 28460715b62e83a89f32db41a9bcafae19f31b5e Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Tue, 27 Sep 2022 20:39:27 +0800 Subject: [PATCH 6/8] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了0019.删除链表的倒数第N个节点 java 版本 --- ...0019.删除链表的倒数第N个节点.md | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 00caeea0..c7f79b8d 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -87,30 +87,31 @@ public: java: ```java -class Solution { - public ListNode removeNthFromEnd(ListNode head, int n) { - ListNode dummy = new ListNode(-1); - dummy.next = head; +public ListNode removeNthFromEnd(ListNode head, int n){ + ListNode dummyNode = new ListNode(0); + dummyNode.next = head; - ListNode slow = dummy; - ListNode fast = dummy; - while (n-- > 0) { - fast = fast.next; - } - // 记住 待删除节点slow 的上一节点 - ListNode prev = null; - while (fast != null) { - prev = slow; - slow = slow.next; - fast = fast.next; - } - // 上一节点的next指针绕过 待删除节点slow 直接指向slow的下一节点 - prev.next = slow.next; - // 释放 待删除节点slow 的next指针, 这句删掉也能AC - slow.next = null; + //排除示例 2 的情况:head = [1], n = 1 + if (head == null) + return null; - return dummy.next; + ListNode fastIndex = dummyNode; + ListNode slowIndex = dummyNode; + + //只要快慢指针相差 n 个结点即可 + for (int i = 0; i < n ; i++){ + fastIndex = fastIndex.next; } + + while (fastIndex.next != null){ + fastIndex = fastIndex.next; + slowIndex = slowIndex.next; + } + + //此时 slowIndex 的位置就是待删除元素的前一个位置。 + //具体情况可自己画一个链表长度为 3 的图来模拟代码来理解 + slowIndex.next = slowIndex.next.next; + return dummyNode.next; } ``` From 87c505371df01e7d62e422b796de66d92754fced Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Tue, 27 Sep 2022 21:01:31 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0019.删除链表的倒数第N个节点.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index c7f79b8d..a84c0b29 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -91,10 +91,6 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode dummyNode = new ListNode(0); dummyNode.next = head; - //排除示例 2 的情况:head = [1], n = 1 - if (head == null) - return null; - ListNode fastIndex = dummyNode; ListNode slowIndex = dummyNode; From 5252cfc060bedce7c5fe460a369343265ab92008 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:40:21 +0800 Subject: [PATCH 8/8] =?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 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 455a2bfe..cdd861fd 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -220,16 +220,17 @@ class ListNode(_x: Int = 0, _next: ListNode = null) { Rust: ```rust -pub struct Node { - value: T, - next: Link, +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: T, + pub next: Option>>, } -type Link = Option>>; - -// 附设头节点 -pub struct List { - head: Link, +impl ListNode { + #[inline] + fn new(val: T, node: Option>>) -> Self { + ListNode { next: node, val } + } } ```