From 68cdbdbb90f2d5a7a32278804f77663198e35a43 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:40:51 -0800 Subject: [PATCH 1/5] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit move second solution to 0496.下一个更大元素I --- problems/0503.下一个更大元素II.md | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index b788968c..6df83fb2 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -170,7 +170,6 @@ class Solution { ### Python: ```python -# 方法 1: class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: dp = [-1] * len(nums) @@ -181,26 +180,6 @@ class Solution: stack.pop() stack.append(i%len(nums)) return dp - -# 方法 2: -class Solution: - def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: - stack = [] - # 创建答案数组 - ans = [-1] * len(nums1) - for i in range(len(nums2)): - while len(stack) > 0 and nums2[i] > nums2[stack[-1]]: - # 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常 - if nums2[stack[-1]] in nums1: - # 锁定 num1 检索的 index - index = nums1.index(nums2[stack[-1]]) - # 更新答案数组 - ans[index] = nums2[i] - # 弹出小元素 - # 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了 - stack.pop() - stack.append(i) - return ans ``` ### Go: From 17a6cb3989befb0d6124f2854c17854dd46d62a1 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:43:00 -0800 Subject: [PATCH 2/5] =?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 Move solution from 0503.下一个更大元素II --- problems/0496.下一个更大元素I.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 45797fb6..d97a3e84 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -256,6 +256,7 @@ class Solution { ### Python3 ```python +# 版本一 class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [-1]*len(nums1) @@ -273,6 +274,26 @@ class Solution: stack.pop() stack.append(i) return result + +# 版本二 +class Solution: + def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: + stack = [] + # 创建答案数组 + ans = [-1] * len(nums1) + for i in range(len(nums2)): + while len(stack) > 0 and nums2[i] > nums2[stack[-1]]: + # 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常 + if nums2[stack[-1]] in nums1: + # 锁定 num1 检索的 index + index = nums1.index(nums2[stack[-1]]) + # 更新答案数组 + ans[index] = nums2[i] + # 弹出小元素 + # 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了 + stack.pop() + stack.append(i) + return ans ``` ### Go From d8f076ef514737ff4ad9247a7380ba2f7c8c7515 Mon Sep 17 00:00:00 2001 From: tlylt Date: Tue, 23 Jan 2024 07:41:36 +0800 Subject: [PATCH 3/5] Add two versions of intersection function in Go --- problems/0349.两个数组的交集.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 26a9286d..25c4e702 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -210,6 +210,8 @@ class Solution: ### Go: +(版本一)使用字典和集合 + ```go func intersection(nums1 []int, nums2 []int) []int { set:=make(map[int]struct{},0) // 用map模拟set @@ -230,6 +232,28 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` +(版本二)使用数组 + +```go +func intersection(nums1 []int, nums2 []int) []int { + count1 := make([]int, 1001, 1001) + count2 := make([]int, 1001, 1001) + res := make([]int, 0) + for _, v := range nums1 { + count1[v] = 1 + } + for _, v := range nums2 { + count2[v] = 1 + } + for i := 0; i <= 1000; i++ { + if count1[i] + count2[i] == 2 { + res = append(res, i) + } + } + return res +} +``` + ### JavaScript: ```js From b0dbf080646b57157fa285559b748a2c8fc8a452 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Mon, 22 Jan 2024 20:45:36 -0800 Subject: [PATCH 4/5] =?UTF-8?q?Update=200127.=E5=8D=95=E8=AF=8D=E6=8E=A5?= =?UTF-8?q?=E9=BE=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix syntax highlight --- problems/0127.单词接龙.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 97bc66d0..6f893310 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -198,7 +198,7 @@ class Solution { ### Python -``` +```python class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: wordSet = set(wordList) From e7e7c4665fe172bcaebed116bb16c3f39e0bc73c Mon Sep 17 00:00:00 2001 From: SteveL <66228787+stevenleon99@users.noreply.github.com> Date: Tue, 23 Jan 2024 21:51:52 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Update=200059.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while (loop --) { i = startx; j = starty; // 下面开始的四个for就是模拟转了一圈 // 模拟填充上行从左到右(左闭右开) for (j; j < n - offset; j++) { res[i][j] = count++; } // 模拟填充右列从上到下(左闭右开) for (i; i < n - offset; i++) { res[i][j] = count++; } --- problems/0059.螺旋矩阵II.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 58378ffc..60281d48 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -87,11 +87,11 @@ public: // 下面开始的四个for就是模拟转了一圈 // 模拟填充上行从左到右(左闭右开) - for (j = starty; j < n - offset; j++) { - res[startx][j] = count++; + for (j; j < n - offset; j++) { + res[i][j] = count++; } // 模拟填充右列从上到下(左闭右开) - for (i = startx; i < n - offset; i++) { + for (i; i < n - offset; i++) { res[i][j] = count++; } // 模拟填充下行从右到左(左闭右开)