Skip to content

Commit

Permalink
'1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Jul 15, 2023
1 parent bdff108 commit 5f8a133
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 66 deletions.
4 changes: 4 additions & 0 deletions notes/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
- [Day 1](./day1.md)
- [704. 二分查找](./day1/lc704.md)
- [27. 移除元素](./day1/lc27.md)
- [Day 2](./day2.md)
- [977. 有序数组的平方](./day2/lc977.md)
- [209. 长度最小的子数组](./day2/lc209.md)
- []
68 changes: 68 additions & 0 deletions notes/src/day1/lc27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 27. 移除元素



## 题目描述
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。


0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100

## 解题思路
线性算法,找到一个要移除的元素就和最后一个交换
```rust
# struct Solution {}

impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let n = nums.len();
if n == 0 { return 0 }
// [i,j) 表示还需要处理的区间,在这个区间之外的都是无需处理的
let mut i = 0;
let mut j = n;
while i < j {
if nums[i] == val {
j -= 1;
nums[i] = nums[j];
} else {
i += 1;
}
}
j as i32
}
}
```


## 学习感想

一开始想的时候其实有不变量的思想在里面

写一下 双指针的版本


```rust
# struct Solution {}
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut a = 0;
let mut b = 0;
let n = nums.len();
while b < n {
if nums[b] == val { b += 1 }
else {
nums[a] = nums[b];
a += 1;
b += 1;
}
}
a as i32
}
}
```
41 changes: 41 additions & 0 deletions notes/src/day2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 第一章 数组part02

977.有序数组的平方 y,209.长度最小的子数组 ,59.螺旋矩阵II ,总结

建议大家先独立做题,然后看视频讲解,然后看文章讲解,然后在重新做一遍题,把题目AC,最后整理成今日当天的博客

拓展题目可以先不做

详细布置

## 977.有序数组的平方

题目建议: 本题关键在于理解双指针思想

题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.html
视频讲解: https://www.bilibili.com/video/BV1QB4y1D7ep

## 209.长度最小的子数组

题目建议: 本题关键在于理解滑动窗口,这个滑动窗口看文字讲解 还挺难理解的,建议大家先看视频讲解。 拓展题目可以先不做。

题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/
文章讲解:https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html
视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE


## 59.螺旋矩阵II

题目建议: 本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。

题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
文章讲解:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html
视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/

总结

题目建议:希望大家 也做一个自己 对数组专题的总结

文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93%E7%AF%87.html

68 changes: 2 additions & 66 deletions notes/src/lc.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
# 27. 移除元素



#
## 题目描述
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。


0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100

## 解题思路
线性算法,找到一个要移除的元素就和最后一个交换
```rust
# struct Solution {}

impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let n = nums.len();
if n == 0 { return 0 }
// [i,j) 表示还需要处理的区间,在这个区间之外的都是无需处理的
let mut i = 0;
let mut j = n;
while i < j {
if nums[i] == val {
j -= 1;
nums[i] = nums[j];
} else {
i += 1;
}
}
j as i32
}
}
```


## 学习感想

一开始想的时候其实有不变量的思想在里面

写一下 双指针的版本


```rust
# struct Solution {}
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut a = 0;
let mut b = 0;
let n = nums.len();
while b < n {
if nums[b] == val { b += 1 }
else {
nums[a] = nums[b];
a += 1;
b += 1;
}
}
a as i32
}
}
```
## 学习感想

0 comments on commit 5f8a133

Please sign in to comment.