Skip to content

Commit 2c1aa6e

Browse files
Update
1 parent e9cdb3e commit 2c1aa6e

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

problems/0202.快乐数.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ object Solution {
417417
}
418418
sum
419419
}
420+
```
420421

421422

422423
C#:

problems/0209.长度最小的子数组.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

77

8-
## 209.长度最小的子数组
8+
# 209.长度最小的子数组
99

1010
[力扣题目链接](https://leetcode-cn.com/problems/minimum-size-subarray-sum/)
1111

@@ -17,6 +17,9 @@
1717
输出:2
1818
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
1919

20+
# 思路
21+
22+
为了易于大家理解,我特意录制了[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE)
2023

2124
## 暴力解法
2225

@@ -47,15 +50,29 @@ public:
4750
}
4851
};
4952
```
50-
时间复杂度:O(n^2)
51-
空间复杂度:O(1)
53+
* 时间复杂度:O(n^2)
54+
* 空间复杂度:O(1)
5255
5356
## 滑动窗口
5457
5558
接下来就开始介绍数组操作中另一个重要的方法:**滑动窗口**。
5659
5760
所谓滑动窗口,**就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果**。
5861
62+
在暴力解法中,是一个for循环滑动窗口的起始位置,一个for循环为滑动窗口的终止位置,用两个for循环 完成了一个不断搜索区间的过程。
63+
64+
那么滑动窗口如何用一个for循环来完成这个操作呢。
65+
66+
首先要思考 如果用一个for循环,那么应该表示 滑动窗口的起始位置,还是终止位置。
67+
68+
如果只用一个for循环来表示 滑动窗口的起始位置,那么如何遍历剩下的终止位置?
69+
70+
此时难免再次陷入 暴力解法的怪圈。
71+
72+
所以 只用一个for循环,那么这个循环的索引,一定是表示 滑动窗口的终止位置。
73+
74+
那么问题来了, 滑动窗口的起始位置如何移动呢?
75+
5976
这里还是以题目中的示例来举例,s=7, 数组是 2,3,1,2,4,3,来看一下查找的过程:
6077
6178
![209.长度最小的子数组](https://code-thinking.cdn.bcebos.com/gifs/209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.gif)
@@ -74,7 +91,7 @@ public:
7491
7592
窗口的起始位置如何移动:如果当前窗口的值大于s了,窗口就要向前移动了(也就是该缩小了)。
7693
77-
窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,窗口的起始位置设置为数组的起始位置就可以了
94+
窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,也就是for循环里的索引
7895
7996
解题的关键在于 窗口的起始位置如何移动,如图所示:
8097
@@ -107,8 +124,8 @@ public:
107124
};
108125
```
109126

110-
时间复杂度:O(n)
111-
空间复杂度:O(1)
127+
* 时间复杂度:O(n)
128+
* 空间复杂度:O(1)
112129

113130
**一些录友会疑惑为什么时间复杂度是O(n)**
114131

problems/0349.两个数组的交集.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ object Solution {
356356
}
357357
}
358358

359+
```
360+
359361

360362
C#:
361363
```csharp

problems/0383.赎金信.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ object Solution {
425425
true
426426
}
427427
}
428+
```
428429

429430

430431
C#:

problems/0454.四数相加II.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ object Solution {
354354
res
355355
}
356356
}
357+
```
357358

358359
C#:
359360
```csharp

problems/qita/server.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
# 一台服务器有什么用!
33

4+
* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
5+
* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
6+
47
但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??**
58

69
这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。

0 commit comments

Comments
 (0)