From ac3f9b8d705a07001590097e0362a6e31dd19b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDraymondHIT=E2=80=9D?= <“3343033352@qq.com”> Date: Sat, 11 Feb 2023 16:58:40 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200028=E6=89=BE=E5=87=BA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=AC=AC=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E9=A1=B9=E7=9A=84=E4=B8=8B=E6=A0=87=20?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E8=A1=A8=EF=BC=88=E4=B8=8D=E5=87=8F=E4=B8=80?= =?UTF-8?q?=EF=BC=89=20Python=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 4e01926f..2757130c 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -769,6 +769,36 @@ class Solution: return next ``` +```python +// 前缀表(不减一)Python实现 +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if len(needle) == 0: + return 0 + next = self.getNext(needle) + j = 0 + for i in range(len(haystack)): + while j >= 1 and haystack[i] != needle[j]: + j = next[j-1] + if haystack[i] == needle[j]: + j += 1 + if j == len(needle): + return i - len(needle) + 1 + return -1 + + def getNext(self, needle): + next = [0] * len(needle) + j = 0 + next[0] = j + for i in range(1, len(needle)): + while j >= 1 and needle[i] != needle[j]: + j = next[j-1] + if needle[i] == needle[j]: + j += 1 + next[i] = j + return next +``` + Go: ```go @@ -1352,3 +1382,4 @@ impl Solution { + From fd505712011729d5abb353073cafaaba0b814e6a Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 13 Feb 2023 17:49:40 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add rust highlighting --- problems/0977.有序数组的平方.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 996a7cee..57f8de02 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -179,7 +179,7 @@ func sortedSquares(nums []int) []int { } ``` Rust -``` +```rust impl Solution { pub fn sorted_squares(nums: Vec) -> Vec { let n = nums.len(); From e41471fdf603d435e58f4b412774b3f6bc2ff82a Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 13 Feb 2023 08:59:42 -0600 Subject: [PATCH 3/6] minor modification --- problems/0509.斐波那契数.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 69306801..08175058 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -219,9 +219,6 @@ class Solution: def fib(self, n: int) -> int: # 排除 Corner Case - if n == 1: - return 1 - if n == 0: return 0 From 633293b509052780afc83b23c07a4ad79ba18156 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:23:52 -0600 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=A3=E9=A2=98?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer58-II.左旋转字符串.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 1b619ffb..49480abf 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -194,6 +194,18 @@ class Solution: ``` +```python 3 +# 方法五:另类的切片方法 +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + n = len(s) + s = s + s + return s[k : n+k] + +# 时间复杂度:O(n) +# 空间复杂度:O(n) +``` + Go: ```go From 20faa53739104f4d979bf4ef4f49894fdd03840a Mon Sep 17 00:00:00 2001 From: KieranTou Date: Wed, 15 Feb 2023 17:03:16 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=B8=BA27.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E5=A2=9E=E5=8A=A0go=E8=AF=AD=E8=A8=80=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=90=91=E5=8F=8C=E6=8C=87=E9=92=88=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 3e26338d..91150c74 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -231,6 +231,31 @@ func removeElement(nums []int, val int) int { return res } ``` +```go +//相向双指针法 +func removeElement(nums []int, val int) int { + // 有点像二分查找的左闭右闭区间 所以下面是<= + left := 0 + right := len(nums) - 1 + for left <= right { + // 不断寻找左侧的val和右侧的非val 找到时交换位置 目的是将val全覆盖掉 + for left <= right && nums[left] != val { + left++ + } + for left <= right && nums[right] == val { + right-- + } + //各自找到后开始覆盖 覆盖后继续寻找 + if left < right { + nums[left] = nums[right] + left++ + right-- + } + } + fmt.Println(nums) + return left +} +``` JavaScript: ```javascript From 4d8363319d431e99ea2bbd4fae2d84a0f9ec17f7 Mon Sep 17 00:00:00 2001 From: Bingyang Yan <75976791+byYanXX@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:43:09 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20=EF=BC=9A=E9=94=99?= =?UTF-8?q?=E5=88=AB=E5=AD=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 应该是想说unordered_map、unordered_set底层实现是哈希表,但写成了unordered_map、unordered_map底层实现是哈希表。 --- problems/二叉树理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 21e2039f..387aa64f 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -72,7 +72,7 @@ 最后一棵 不是平衡二叉树,因为它的左右两个子树的高度差的绝对值超过了1。 -**C++中map、set、multimap,multiset的底层实现都是平衡二叉搜索树**,所以map、set的增删操作时间时间复杂度是logn,注意我这里没有说unordered_map、unordered_set,unordered_map、unordered_map底层实现是哈希表。 +**C++中map、set、multimap,multiset的底层实现都是平衡二叉搜索树**,所以map、set的增删操作时间时间复杂度是logn,注意我这里没有说unordered_map、unordered_set,unordered_map、unordered_set底层实现是哈希表。 **所以大家使用自己熟悉的编程语言写算法,一定要知道常用的容器底层都是如何实现的,最基本的就是map、set等等,否则自己写的代码,自己对其性能分析都分析不清楚!**