From 83096415a05cd5c6b1cb6a0269ca8327e993148a Mon Sep 17 00:00:00 2001 From: Galaxies2580 Date: Mon, 26 Sep 2022 09:14:58 +0800 Subject: [PATCH 01/36] 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 02/36] =?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 03/36] =?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 04/36] =?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 05/36] =?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 06/36] =?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 07/36] =?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 08/36] =?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 } + } } ``` From ca4c4f7f8be13af4ec8106fdc6e07ca26d6927c1 Mon Sep 17 00:00:00 2001 From: ZGQ Date: Wed, 28 Sep 2022 20:07:51 +0800 Subject: [PATCH 09/36] =?UTF-8?q?Update=200129.=E6=B1=82=E6=A0=B9=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=88=B0=E5=8F=B6=E8=8A=82=E7=82=B9=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0129.求根到叶子节点数字之和.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 859df39e..bd5107a2 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -320,8 +320,11 @@ TypeScript: ```typescript function sumNumbers(root: TreeNode | null): number { if (root === null) return 0; + // 记录最终结果 let resTotal: number = 0; + // 记录路径中遇到的节点值 const route: number[] = []; + // 递归初始值 route.push(root.val); recur(root, route); return resTotal; @@ -342,6 +345,7 @@ function sumNumbers(root: TreeNode | null): number { }; } function listToSum(nums: number[]): number { + // 数组求和 return Number(nums.join('')); } }; From de017e95441ece804182aec87cfba7f4414cc4a7 Mon Sep 17 00:00:00 2001 From: zihao Tan <53686585+Tcotyledons@users.noreply.github.com> Date: Thu, 29 Sep 2022 09:41:25 +0800 Subject: [PATCH 10/36] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 104二叉树最大深度贡献java版本的从深度角度的递归法 --- problems/0104.二叉树的最大深度.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 2bcc9c5c..bcf3a4a3 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -313,6 +313,31 @@ class solution { } ``` +```java +class Solution { + /** + * 递归法(求深度法) + */ + //定义最大深度 + int maxnum = 0; + + public int maxDepth(TreeNode root) { + ans(root,0); + return maxnum; + } + + //递归求解最大深度 + void ans(TreeNode tr,int tmp){ + if(tr==null) return; + tmp++; + maxnum = maxnum Date: Thu, 29 Sep 2022 10:51:23 +0800 Subject: [PATCH 11/36] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 8107e4e0..257bbb9a 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -89,26 +89,32 @@ C++代码如下: class Solution { public: int evalRPN(vector& tokens) { - stack st; + // 考虑到第20个样例使用int会溢出 + // 此处使用long long来存储number + // 在最后用int()强行转换成int输出 + + stack st; for (int i = 0; i < tokens.size(); i++) { if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { - int num1 = st.top(); + long long num1 = st.top(); st.pop(); - int num2 = st.top(); + long long num2 = st.top(); st.pop(); if (tokens[i] == "+") st.push(num2 + num1); if (tokens[i] == "-") st.push(num2 - num1); if (tokens[i] == "*") st.push(num2 * num1); if (tokens[i] == "/") st.push(num2 / num1); } else { - st.push(stoi(tokens[i])); + st.push(atoll(tokens[i].c_str())); } } - int result = st.top(); + + long long result = st.top(); st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事) - return result; + return int(result); } }; + ``` ## 题外话 From 4d6b8252a1d51b0cef64e5b4739ef21329bdd41c Mon Sep 17 00:00:00 2001 From: chenzhg <2216468566@qq.com> Date: Thu, 29 Sep 2022 17:31:58 +0800 Subject: [PATCH 12/36] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=20C++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0583.两个字符串的删除操作.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index fd80853e..c33f7f58 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -47,6 +47,8 @@ dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word 那最后当然是取最小值,所以当word1[i - 1] 与 word2[j - 1]不相同的时候,递推公式:dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); +因为dp[i - 1][j - 1] + 1等于 dp[i - 1][j] 或 dp[i][j - 1],所以递推公式可简化为:dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1); + 3. dp数组如何初始化 @@ -90,7 +92,7 @@ public: if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { - dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); + dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1); } } } From 755ba1b0786dbc3fad56f31c54de7f4727b23819 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Thu, 29 Sep 2022 18:34:49 +0800 Subject: [PATCH 13/36] =?UTF-8?q?update=20707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 88 +++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 83e57fec..1f7e6cbf 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -1240,6 +1240,94 @@ class MyLinkedList() { } ``` +Rust: + +```rust +#[derive(Debug)] +pub struct MyLinkedList { + pub val: i32, + pub next: Option>, +} + +impl MyLinkedList { + fn new() -> Self { + // 增加头节点 + MyLinkedList { val: 0, next: None } + } + + fn get(&self, index: i32) -> i32 { + if index < 0 { + return -1; + } + let mut i = 0; + let mut cur = &self.next; + while let Some(node) = cur { + if i == index { + return node.val; + } + i += 1; + cur = &node.next; + } + -1 + } + + fn add_at_head(&mut self, val: i32) { + let new_node = Box::new(MyLinkedList { + val, + next: self.next.take(), + }); + self.next = Some(new_node); + } + + fn add_at_tail(&mut self, val: i32) { + let new_node = Box::new(MyLinkedList { val, next: None }); + let mut last_node = &mut self.next; + while let Some(node) = last_node { + last_node = &mut node.next; + } + *last_node = Some(new_node); + } + + fn add_at_index(&mut self, index: i32, val: i32) { + if index <= 0 { + self.add_at_head(val); + } else { + let mut i = 0; + let mut cur = &mut self.next; + while let Some(node) = cur { + if i + 1 == index { + let new_node = Box::new(MyLinkedList { + val, + next: node.next.take(), + }); + node.next = Some(new_node); + break; + } + i += 1; + cur = &mut node.next; + } + } + } + + fn delete_at_index(&mut self, index: i32) { + if index < 0 { + return; + } + + let mut i = 0; + let mut cur = self; + while let Some(node) = cur.next.take() { + if i == index { + cur.next = node.next; + break; + } + i += 1; + cur.next = Some(node); + cur = cur.next.as_mut().unwrap(); + } + } +} +``` -----------------------
From 12f2ecf564c232edcd4a0d1eb143a9aa9aed05d3 Mon Sep 17 00:00:00 2001 From: vanyongqi <46806467+vanyongqi@users.noreply.github.com> Date: Fri, 30 Sep 2022 10:20:50 +0800 Subject: [PATCH 14/36] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加C语言的两种版本,已通过Leetcode测试 --- problems/0035.搜索插入位置.md | 52 ++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 4b17bec6..98984f9e 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -429,7 +429,57 @@ function searchInsert($nums, $target) return $r + 1; } ``` - +### C +```c +//版本一 [left, right]左闭右闭区间 +int searchInsert(int* nums, int numsSize, int target){ + //左闭右开区间 [0 , numsSize-1] + int left =0; + int mid =0; + int right = numsSize - 1; + while(left <= right){//左闭右闭区间 所以可以 left == right + mid = left + (right - left) / 2; + if(target < nums[mid]){ + //target 在左区间 [left, middle - 1]中,原区间包含mid,右区间边界可以向左内缩 + right = mid -1; + }else if( target > nums[mid]){ + // target 在右区间 [mid + 1, right]中,原区间包含mid,左区间边界可以向右内缩 + left = mid + 1; + }else { + // nums[mid] == target ,顺利找到target,直接返回mid + return mid; + } + } + //数组中未找到target元素 + //target在数组所有元素之后,[left, right]是右闭区间,需要返回 right +1 + return right + 1; +} +``` +```c +//版本二 [left, right]左闭右开区间 +int searchInsert(int* nums, int numsSize, int target){ + //左闭右开区间 [0 , numsSize) + int left =0; + int mid =0; + int right = numsSize; + while(left < right){//左闭右闭区间 所以 left < right + mid = left + (right - left) / 2; + if(target < nums[mid]){ + //target 在左区间 [left, mid)中,原区间没有包含mid,右区间边界可以不能内缩 + right = mid ; + }else if( target > nums[mid]){ + // target 在右区间 [mid+1, right)中,原区间包含mid,左区间边界可以向右内缩 + left = mid + 1; + }else { + // nums[mid] == target ,顺利找到target,直接返回mid + return mid; + } + } + //数组中未找到target元素 + //target在数组所有元素之后,[left, right)是右开区间, return right即可 + return right; +} +``` -----------------------
From 4276745f1ba4b048c022013edddb89d3959fd49b Mon Sep 17 00:00:00 2001 From: vanyongqi <46806467+vanyongqi@users.noreply.github.com> Date: Fri, 30 Sep 2022 10:23:25 +0800 Subject: [PATCH 15/36] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加C语言两种版本,通过Leetcode测试 --- problems/0035.搜索插入位置.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 98984f9e..cd2c88eb 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -440,10 +440,10 @@ int searchInsert(int* nums, int numsSize, int target){ while(left <= right){//左闭右闭区间 所以可以 left == right mid = left + (right - left) / 2; if(target < nums[mid]){ - //target 在左区间 [left, middle - 1]中,原区间包含mid,右区间边界可以向左内缩 + //target 在左区间 [left, mid - 1]中,原区间包含mid,右区间边界可以向左内缩 right = mid -1; }else if( target > nums[mid]){ - // target 在右区间 [mid + 1, right]中,原区间包含mid,左区间边界可以向右内缩 + //target 在右区间 [mid + 1, right]中,原区间包含mid,左区间边界可以向右内缩 left = mid + 1; }else { // nums[mid] == target ,顺利找到target,直接返回mid @@ -465,7 +465,7 @@ int searchInsert(int* nums, int numsSize, int target){ while(left < right){//左闭右闭区间 所以 left < right mid = left + (right - left) / 2; if(target < nums[mid]){ - //target 在左区间 [left, mid)中,原区间没有包含mid,右区间边界可以不能内缩 + //target 在左区间 [left, mid)中,原区间没有包含mid,右区间边界不能内缩 right = mid ; }else if( target > nums[mid]){ // target 在右区间 [mid+1, right)中,原区间包含mid,左区间边界可以向右内缩 From 2772fd498da7160313210e1b58b2b86d907ac96a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=82=E8=AC=99?= Date: Fri, 30 Sep 2022 14:48:18 +0800 Subject: [PATCH 16/36] =?UTF-8?q?update=20455.=E5=88=86=E7=99=BC=E9=A4=85?= =?UTF-8?q?=E4=B9=BE=20C=E8=AA=9E=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index f9e8e7f1..93a4c2d3 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -274,6 +274,7 @@ function findContentChildren(g: number[], s: number[]): number { ### C ```c +///小餅乾先餵飽小胃口的 int cmp(int* a, int* b) { return *a - *b; } @@ -296,6 +297,33 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){ } ``` +```c +///大餅乾先餵飽大胃口的 +int cmp(int* a, int* b) { + return *a - *b; +} + +int findContentChildren(int* g, int gSize, int* s, int sSize){ + if(sSize == 0) + return 0; + + //将两个数组排序为升序 + qsort(g, gSize, sizeof(int), cmp); + qsort(s, sSize, sizeof(int), cmp); + + int count = 0; + int start = sSize - 1; + + for(int i = gSize - 1; i >= 0; i--) { + if(start >= 0 && s[start] >= g[i] ) { + start--; + count++; + } + } + return count; +} +``` + ### Scala ```scala From a8a98842cb6deb7c365e5e47790b1472abe064ad Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:36:16 +0800 Subject: [PATCH 17/36] =?UTF-8?q?update=20206=E7=BF=BB=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index e97befee..009b4469 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -588,5 +588,45 @@ object Solution { } ``` + +Rust: +双指针法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + let mut cur = head; + let mut pre = None; + while let Some(mut node) = cur.take() { + cur = node.next; + node.next = pre; + pre = Some(node); + } + pre + } +} +``` + +递归法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + fn rev( + mut head: Option>, + mut pre: Option>, + ) -> Option> { + if let Some(mut node) = head.take() { + let cur = node.next; + node.next = pre; + pre = Some(node); + return rev(cur, pre); + } + pre + } + rev(head, None) + } +} +``` -----------------------
From 987380ca9327a5e097d1b0a7661586eeb2f088d2 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:32:22 +0800 Subject: [PATCH 18/36] =?UTF-8?q?update=200024.=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 10337a7f..3b86ac6f 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -380,5 +380,30 @@ function swapPairs($head) } ``` +Rust: + +```rust +// 虚拟头节点 +impl Solution { + pub fn swap_pairs(head: Option>) -> Option> { + let mut dummy_head = Box::new(ListNode::new(0)); + dummy_head.next = head; + let mut cur = dummy_head.as_mut(); + while let Some(mut node) = cur.next.take() { + if let Some(mut next) = node.next.take() { + node.next = next.next.take(); + next.next = Some(node); + cur.next = Some(next); + cur = cur.next.as_mut().unwrap().next.as_mut().unwrap(); + } else { + cur.next = Some(node); + cur = cur.next.as_mut().unwrap(); + } + } + dummy_head.next + } +} +``` + -----------------------
From 00ad23f45815ca3a371b6beae65da209433708cf Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 1 Oct 2022 15:23:53 +0800 Subject: [PATCH 19/36] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 3b86ac6f..d65d0bca 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -405,5 +405,26 @@ impl Solution { } ``` +```rust +// 递归 +impl Solution { + pub fn swap_pairs(head: Option>) -> Option> { + if head == None || head.as_ref().unwrap().next == None { + return head; + } + + let mut node = head.unwrap(); + + if let Some(mut next) = node.next.take() { + node.next = Solution::swap_pairs(next.next); + next.next = Some(node); + Some(next) + } else { + Some(node) + } + } +} +``` + -----------------------
From c58be464b4ca65d6d396c744dafbc3df984bf206 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 1 Oct 2022 16:21:39 +0800 Subject: [PATCH 20/36] =?UTF-8?q?update=20[19]=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC=20N=20?= =?UTF-8?q?=E4=B8=AA=E7=BB=93=E7=82=B9=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index a84c0b29..b9859968 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -338,5 +338,28 @@ object Solution { } } ``` + +Rust: +```rust +impl Solution { + pub fn remove_nth_from_end(head: Option>, mut n: i32) -> Option> { + let mut dummy_head = Box::new(ListNode::new(0)); + dummy_head.next = head; + let mut fast = &dummy_head.clone(); + let mut slow = &mut dummy_head; + while n > 0 { + fast = fast.next.as_ref().unwrap(); + n -= 1; + } + while fast.next.is_some() { + fast = fast.next.as_ref().unwrap(); + slow = slow.next.as_mut().unwrap(); + } + slow.next = slow.next.as_mut().unwrap().next.take(); + dummy_head.next + } +} +``` + -----------------------
From e4ca3482fbb5202172c6268a10565dfbf116f6fc Mon Sep 17 00:00:00 2001 From: chenzhg <2216468566@qq.com> Date: Sat, 1 Oct 2022 16:25:03 +0800 Subject: [PATCH 21/36] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB=20=E6=B7=BB=E5=8A=A0C=E8=AF=AD=E8=A8=80=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/0072.编辑距离.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index e40461de..a15012d7 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -362,6 +362,33 @@ function minDistance(word1: string, word2: string): number { }; ``` +C: + +```c +int min(int num1, int num2, int num3) { + return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1); +} + +int minDistance(char * word1, char * word2){ + int dp[strlen(word1)+1][strlen(word2)+1]; + dp[0][0] = 0; + for (int i = 1; i <= strlen(word1); i++) dp[i][0] = i; + for (int i = 1; i <= strlen(word2); i++) dp[0][i] = i; + + for (int i = 1; i <= strlen(word1); i++) { + for (int j = 1; j <=strlen(word2); j++) { + if (word1[i-1] == word2[j-1]) { + dp[i][j] = dp[i-1][j-1]; + } + else { + dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1; + } + } + } + return dp[strlen(word1)][strlen(word2)]; +} +``` + ----------------------- From 3374f8e875cdd0e77bfbc3db03fcaa497029b817 Mon Sep 17 00:00:00 2001 From: ZGQ Date: Sat, 1 Oct 2022 16:35:27 +0800 Subject: [PATCH 22/36] =?UTF-8?q?feat:0090.=E5=AD=90=E9=9B=86=E2=85=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Typescript=E7=9A=84set=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 9e7e3bd0..81c0f94a 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -367,6 +367,39 @@ function subsetsWithDup(nums: number[]): number[][] { }; ``` +set去重版本: +```typescript +// 使用set去重版本 +function subsetsWithDup(nums: number[]): number[][] { + const result: number[][] = []; + const path: number[] = []; + // 去重之前先排序 + nums.sort((a, b) => a - b); + function backTracking(startIndex: number) { + // 收集结果 + result.push([...path]) + // 此处不返回也可以因为,每次递归都会使startIndex + 1,当这个数大到nums.length的时候就不会进入递归了。 + if (startIndex === nums.length) { + return + } + // 定义每一个树层的set集合 + const set: Set = new Set() + for (let i = startIndex; i < nums.length; i++) { + // 去重 + if (set.has(nums[i])) { + continue + } + set.add(nums[i]) + path.push(nums[i]) + backTracking(i + 1) + // 回溯 + path.pop() + } + } + backTracking(0) + return result +}; +``` ### Rust ```Rust From e4ede152f7d21f8ee66135119b9f0318c77bd34c Mon Sep 17 00:00:00 2001 From: chenzhg <2216468566@qq.com> Date: Sat, 1 Oct 2022 16:49:23 +0800 Subject: [PATCH 23/36] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0072.编辑距离.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index e40461de..df5d37c2 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -362,7 +362,32 @@ function minDistance(word1: string, word2: string): number { }; ``` +C: +```c +int min(int num1, int num2, int num3) { + return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1); +} + +int minDistance(char * word1, char * word2){ + int dp[strlen(word1)+1][strlen(word2)+1]; + dp[0][0] = 0; + for (int i = 1; i <= strlen(word1); i++) dp[i][0] = i; + for (int i = 1; i <= strlen(word2); i++) dp[0][i] = i; + + for (int i = 1; i <= strlen(word1); i++) { + for (int j = 1; j <= strlen(word2); j++) { + if (word1[i-1] == word2[j-1]) { + dp[i][j] = dp[i-1][j-1]; + } + else { + dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1; + } + } + } + return dp[strlen(word1)][strlen(word2)]; +} +``` -----------------------
From b0ec03922ea2081f0aa72e7fccf8a322aa9a9efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 2 Oct 2022 10:46:19 +0800 Subject: [PATCH 24/36] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 力扣修改了后端测试数据 --- problems/0150.逆波兰表达式求值.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 257bbb9a..a0ec5db3 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -89,10 +89,7 @@ C++代码如下: class Solution { public: int evalRPN(vector& tokens) { - // 考虑到第20个样例使用int会溢出 - // 此处使用long long来存储number - // 在最后用int()强行转换成int输出 - + // 力扣修改了后台测试数据,需要用longlong stack st; for (int i = 0; i < tokens.size(); i++) { if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { @@ -105,13 +102,13 @@ public: if (tokens[i] == "*") st.push(num2 * num1); if (tokens[i] == "/") st.push(num2 / num1); } else { - st.push(atoll(tokens[i].c_str())); + st.push(stoll(tokens[i])); } } - long long result = st.top(); + int result = st.top(); st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事) - return int(result); + return result; } }; From f55786bdb2c01e3618d8b5b5b2d046e0581a5d89 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:24:52 +0800 Subject: [PATCH 25/36] =?UTF-8?q?Update=EF=BC=9A=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md=20java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=20=E5=8F=98=E9=87=8F=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=92=8C=E5=91=BD=E5=90=8D=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 159 行 StringBuffer str 改成 String s 并把后续的 str 都换成 s 不然原答案是不能AC 的 --- problems/剑指Offer05.替换空格.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index e1ccc458..7a2712ef 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -156,20 +156,20 @@ char* replaceSpace(char* s){ Java: ```Java //使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制 -public static String replaceSpace(StringBuffer str) { - if (str == null) { +public static String replaceSpace(String s) { + if (s == null) { return null; } - //选用 StringBuilder 单线程使用,比较快,选不选都行 + //选用 StringBuilder 单线程使用,比较快,选不选都行 StringBuilder sb = new StringBuilder(); - //使用 sb 逐个复制 str ,碰到空格则替换,否则直接复制 - for (int i = 0; i < str.length(); i++) { - //str.charAt(i) 为 char 类型,为了比较需要将其转为和 " " 相同的字符串类型 - //if (" ".equals(String.valueOf(str.charAt(i)))){ + //使用 sb 逐个复制 s ,碰到空格则替换,否则直接复制 + for (int i = 0; i < s.length(); i++) { + //s.charAt(i) 为 char 类型,为了比较需要将其转为和 " " 相同的字符串类型 + //if (" ".equals(String.valueOf(s.charAt(i)))){} if (s.charAt(i) == ' ') { sb.append("%20"); } else { - sb.append(str.charAt(i)); + sb.append(s.charAt(i)); } } return sb.toString(); From e9a164add1f9c82318597bfbbc10737f700a8298 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 3 Oct 2022 17:59:23 +0800 Subject: [PATCH 26/36] =?UTF-8?q?update=EF=BC=9A0151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md=20java=20=E7=89=88=E6=9C=AC=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E5=9B=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 时间复杂度 O(n) 参考卡哥 c++ 代码的实现:先移除多余空格,再将整个字符串反转,最后把单词逐个反转 --- problems/0151.翻转字符串里的单词.md | 68 ++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index a848d6a3..5a8f8094 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -360,6 +360,74 @@ class Solution { } ``` +```java +/* + * 解法四:时间复杂度 O(n) + * 参考卡哥 c++ 代码的实现:先移除多余空格,再将整个字符串反转,最后把单词逐个反转 + */ +class Solution { + //用 char[] 来实现 String 的 removeExtraSpaces,reverse 操作 + public String reverseWords(String s) { + char[] chars = s.toCharArray(); + //1.去除首尾以及中间多余空格 + chars = removeExtraSpaces(chars); + //2.整个字符串反转 + reverse(chars, 0, chars.length - 1); + //3.单词反转 + reverseEachWord(chars); + return new String(chars); + } + + //1.用 快慢指针 去除首尾以及中间多余空格,可参考数组元素移除的题解 + public char[] removeExtraSpaces(char[] chars) { + int slow = 0; + for (int fast = 0; fast < chars.length; fast++) { + //先用 fast 移除所有空格 + if (chars[fast] != ' ') { + //在用 slow 加空格。 除第一个单词外,单词末尾要加空格 + if (slow != 0) + chars[slow++] = ' '; + //fast 遇到空格或遍历到字符串末尾,就证明遍历完一个单词了 + while (fast < chars.length && chars[fast] != ' ') + chars[slow++] = chars[fast++]; + } + } + //相当于 c++ 里的 resize() + char[] newChars = new char[slow]; + System.arraycopy(chars, 0, newChars, 0, slow); + return newChars; + } + + //双指针实现指定范围内字符串反转,可参考字符串反转题解 + public void reverse(char[] chars, int left, int right) { + if (right >= chars.length) { + System.out.println("set a wrong right"); + return; + } + while (left < right) { + chars[left] ^= chars[right]; + chars[right] ^= chars[left]; + chars[left] ^= chars[right]; + left++; + right--; + } + } + + //3.单词反转 + public void reverseEachWord(char[] chars) { + int start = 0; + //end <= s.length() 这里的 = ,是为了让 end 永远指向单词末尾后一个位置,这样 reverse 的实参更好设置 + for (int end = 0; end <= chars.length; end++) { + // end 每次到单词末尾后的空格或串尾,开始反转单词 + if (end == chars.length || chars[end] == ' ') { + reverse(chars, start, end - 1); + start = end + 1; + } + } + } +} +``` + python: ```Python From 42d72e2a313cc0409b183923c1d3d8d9bcc38fdf Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 3 Oct 2022 18:14:34 +0800 Subject: [PATCH 27/36] =?UTF-8?q?update=EF=BC=9A0151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md=20java=20=E7=89=88=E6=9C=AC=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E5=9B=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 时间复杂度 O(n) 参考卡哥 c++ 代码的实现:先移除多余空格,再将整个字符串反转,最后把单词逐个反转 有别于解法一 :没有用 StringBuilder 实现,而是对 String 的 char[] 数组操作来实现以上三个步骤 --- problems/0151.翻转字符串里的单词.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 5a8f8094..be35c2f2 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -363,7 +363,8 @@ class Solution { ```java /* * 解法四:时间复杂度 O(n) - * 参考卡哥 c++ 代码的实现:先移除多余空格,再将整个字符串反转,最后把单词逐个反转 + * 参考卡哥 c++ 代码的三步骤:先移除多余空格,再将整个字符串反转,最后把单词逐个反转 + * 有别于解法一 :没有用 StringBuilder 实现,而是对 String 的 char[] 数组操作来实现以上三个步骤 */ class Solution { //用 char[] 来实现 String 的 removeExtraSpaces,reverse 操作 From b24e3f04ac05798947ac1bd580c7a094c3353e19 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 3 Oct 2022 19:37:53 +0800 Subject: [PATCH 28/36] =?UTF-8?q?update=EF=BC=9A=E5=89=91=E6=8C=87Offer58-?= =?UTF-8?q?II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解法二:空间复杂度:O(1)。用原始数组来进行反转操作 思路:先整个字符串反转,再反转前面的,最后反转后面 n 个 --- .../剑指Offer58-II.左旋转字符串.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index bf5d3f90..b14c26cc 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -115,6 +115,29 @@ class Solution { } ``` +```java +//解法二:空间复杂度:O(1)。用原始数组来进行反转操作 +//思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 +class Solution { + public String reverseLeftWords(String s, int n) { + char[] chars = s.toCharArray(); + reverse(chars, 0, chars.length - 1); + reverse(chars, 0, chars.length - 1 - n); + reverse(chars, chars.length - n, chars.length - 1); + return new String(chars); + } + + public void reverse(char[] chars, int left, int right) { + while (left < right) { + chars[left] ^= chars[right]; + chars[right] ^= chars[left]; + chars[left] ^= chars[right]; + left++; + right--; + } + } +``` + python: ```python From f86b17a1f09915988a2a6dda54f4b8a9b1d5dca7 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Tue, 4 Oct 2022 21:40:54 +0800 Subject: [PATCH 29/36] =?UTF-8?q?update=EF=BC=9A=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E9=92=88=E6=80=BB=E7=BB=93.md=20=E6=9B=B4=E6=94=B9=E9=94=99?= =?UTF-8?q?=E5=AD=97?= 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 06752cac..5abcec53 100644 --- a/problems/双指针总结.md +++ b/problems/双指针总结.md @@ -42,7 +42,7 @@ for (int i = 0; i < array.size(); i++) { **其实很多数组(字符串)填充类的问题,都可以先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** -那么在[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中,我们使用双指针法,用O(n)的时间复杂度完成字符串删除类的操作,因为题目要产出冗余空格。 +那么在[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中,我们使用双指针法,用O(n)的时间复杂度完成字符串删除类的操作,因为题目要删除冗余空格。 **在删除冗余空格的过程中,如果不注意代码效率,很容易写成了O(n^2)的时间复杂度。其实使用双指针法O(n)就可以搞定。** From 680cf3dbd74d4266d1a077c493d0c440fd6bebfb Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Wed, 5 Oct 2022 10:12:19 +0800 Subject: [PATCH 30/36] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20java=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化,使用一个 Queue 实现 对比 『使用一个 Deque 实现』 的解法,我把移动元素的代码放到 push 方法里,代码看起来更简洁 --- problems/0225.用队列实现栈.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index fb2851f1..4412a819 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -292,6 +292,40 @@ class MyStack { } ``` +优化,使用一个 Queue 实现 +```java +class MyStack { + + Queue queue; + + public MyStack() { + queue = new LinkedList<>(); + } + + //每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首 + public void push(int x) { + queue.offer(x); + int size = queue.size(); + //移动除了 A 的其它数 + while (size-- > 1) + queue.offer(queue.poll()); + } + + public int pop() { + return queue.poll(); + } + + public int top() { + return queue.peek(); + } + + public boolean empty() { + return queue.isEmpty(); + } +} + +``` + Python: ```python From 6476576fa7d88790ad881daecc05303a82913058 Mon Sep 17 00:00:00 2001 From: wyzzoo Date: Thu, 6 Oct 2022 00:17:27 +0800 Subject: [PATCH 31/36] =?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=EF=BC=8C=E4=BF=AE=E5=A4=8D=20CPP=20?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=BB=A3=E7=A0=81=E4=B8=AD=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0019.删除链表的倒数第N个节点.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index a84c0b29..a37ef1d3 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -75,7 +75,11 @@ public: fast = fast->next; slow = slow->next; } - slow->next = slow->next->next; + + ListNode *nth = slow->next; + slow->next = nth->next; + delete nth; + return dummyHead->next; } }; From 1889a033c3b6754bdaafecf9ab6665deda1bee19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=82=E8=AC=99?= Date: Fri, 7 Oct 2022 11:18:22 +0800 Subject: [PATCH 32/36] =?UTF-8?q?add=20001.=E5=85=A9=E6=95=B8=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20C=E8=AA=9E=E8=A8=80(=E4=BD=BF=E7=94=A8=20leetcode?= =?UTF-8?q?=20=E6=94=AF=E6=8F=B4=E4=B9=8B=20ut=5Fhash=20=E5=87=BD=E5=BC=8F?= =?UTF-8?q?=E5=BA=AB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 5bedd0a0..bc618c15 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -352,5 +352,86 @@ List twoSum(List nums, int target) { } ``` +C: +```c + + +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ + +// leetcode 支持 ut_hash 函式庫 + + typedef struct { + int key; + int value; + UT_hash_handle hh; // make this structure hashable + } map; + +map* hashMap = NULL; + + void hashMapAdd(int key, int value){ + map* s; + // key already in the hash? + HASH_FIND_INT(hashMap, &key, s); + if(s == NULL){ + s = (map*)malloc(sizeof(map)); + s -> key = key; + HASH_ADD_INT(hashMap, key, s); + } + s -> value = value; + } + +map* hashMapFind(int key){ + map* s; + // *s: output pointer + HASH_FIND_INT(hashMap, &key, s); + return s; + } + + void hashMapCleanup(){ + map* cur, *tmp; + HASH_ITER(hh, hashMap, cur, tmp){ + HASH_DEL(hashMap, cur); + free(cur); + } + } + + void hashPrint(){ + map* s; + for(s = hashMap; s != NULL; s=(map*)(s -> hh.next)){ + printf("key %d, value %d\n", s -> key, s -> value); + } + } + + +int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + int i, *ans; + // hash find result + map* hashMapRes; + hashMap = NULL; + ans = malloc(sizeof(int) * 2); + + for(i = 0; i < numsSize; i++){ + // key 代表 nums[i] 的值,value 代表所在 index; + hashMapAdd(nums[i], i); + } + + hashPrint(); + + for(i = 0; i < numsSize; i++){ + hashMapRes = hashMapFind(target - nums[i]); + if(hashMapRes && hashMapRes -> value != i){ + ans[0] = i; + ans[1] = hashMapRes -> value ; + *returnSize = 2; + return ans; + } + } + + hashMapCleanup(); + return NULL; +} +``` -----------------------
From 3223112124c71c21fef1354a451b12a64c118f7d Mon Sep 17 00:00:00 2001 From: KiloG <33695125+JOJO0527@users.noreply.github.com> Date: Fri, 7 Oct 2022 21:26:39 +0800 Subject: [PATCH 33/36] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Go=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E5=A4=96=E4=B8=80=E7=A7=8D=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 可以将题目转化为“求装满背包s的前几位字符的方式有几种”, 然后判断最后dp[len(s)]是否大于0就可。 --- problems/0139.单词拆分.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index d60fd46c..5226502a 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -344,6 +344,21 @@ func wordBreak(s string,wordDict []string) bool { } return dp[len(s)] } +// 转化为 求装满背包s的前几位字符的方式有几种 +func wordBreak(s string, wordDict []string) bool { + // 装满背包s的前几位字符的方式有几种 + dp := make([]int, len(s)+1) + dp[0] = 1 + for i := 0; i <= len(s); i++ { // 背包 + for j := 0; j < len(wordDict); j++ { // 物品 + if i >= len(wordDict[j]) && wordDict[j] == s[i-len(wordDict[j]):i] { + dp[i] += dp[i-len(wordDict[j])] + } + } + } + + return dp[len(s)] > 0 +} ``` Javascript: From a918d6fa07f4c5d0b3247b242ef30a3aacdff5fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E6=9E=97=E7=A8=8B=E5=BA=8F=E5=91=98?= Date: Sat, 8 Oct 2022 11:45:40 +0800 Subject: [PATCH 34/36] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dpython=E5=9C=A8md?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E8=AF=AD=E6=B3=95=E9=AB=98=E4=BA=AE&?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 2a018736..0bcd31e5 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -181,23 +181,23 @@ class Solution: index += 1 return 0 if res==float("inf") else res ``` -```python3 -#滑动窗口 +```python +# 滑动窗口 class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: - if nums is None or len(nums)==0: + if nums is None or len(nums) == 0: return 0 - lenf=len(nums)+1 - total=0 - i=j=0 - while (j=target): - lenf=min(lenf,j-i) - total=total-nums[i] - i+=1 - if lenf==len(nums)+1: + 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] + i += 1 + if lenf == len(nums) + 1: return 0 else: return lenf From b656e6032e85cc18970e0ad521e5798b67c03b51 Mon Sep 17 00:00:00 2001 From: Liu YongLiang <41845017+tlylt@users.noreply.github.com> Date: Sun, 9 Oct 2022 09:14:47 +0800 Subject: [PATCH 35/36] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1049.最后一块石头的重量II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index b6a674aa..728ae83e 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -43,7 +43,7 @@ 是不是感觉和昨天讲解的[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)非常像了。 -本题物品的重量为store[i],物品的价值也为store[i]。 +本题物品的重量为stones[i],物品的价值也为stones[i]。 对应着01背包里的物品重量weight[i]和 物品价值value[i]。 From 35b28d376820dbc014010e8898430a46213e27e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Wed, 12 Oct 2022 12:32:40 +0800 Subject: [PATCH 36/36] =?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 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index a37ef1d3..18d4a057 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -75,10 +75,11 @@ public: fast = fast->next; slow = slow->next; } + slow->next = slow->next->next; - ListNode *nth = slow->next; - slow->next = nth->next; - delete nth; + // ListNode *tmp = slow->next; C++释放内存的逻辑 + // slow->next = tmp->next; + // delete nth; return dummyHead->next; }