Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2023-02-18 09:18:57 +08:00
6 changed files with 70 additions and 5 deletions

View File

@ -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

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -219,9 +219,6 @@ class Solution:
def fib(self, n: int) -> int:
# 排除 Corner Case
if n == 1:
return 1
if n == 0:
return 0

View File

@ -179,7 +179,7 @@ func sortedSquares(nums []int) []int {
}
```
Rust
```
```rust
impl Solution {
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();

View File

@ -72,7 +72,7 @@
最后一棵 不是平衡二叉树因为它的左右两个子树的高度差的绝对值超过了1。
**C++中map、set、multimapmultiset的底层实现都是平衡二叉搜索树**所以map、set的增删操作时间时间复杂度是logn注意我这里没有说unordered_map、unordered_setunordered_map、unordered_map底层实现是哈希表。
**C++中map、set、multimapmultiset的底层实现都是平衡二叉搜索树**所以map、set的增删操作时间时间复杂度是logn注意我这里没有说unordered_map、unordered_setunordered_map、unordered_set底层实现是哈希表。
**所以大家使用自己熟悉的编程语言写算法一定要知道常用的容器底层都是如何实现的最基本的就是map、set等等否则自己写的代码自己对其性能分析都分析不清楚**

View File

@ -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