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 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 { + 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 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(); 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等等,否则自己写的代码,自己对其性能分析都分析不清楚!** 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