Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9d679b

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 26cdabc + 07e0016 commit a9d679b

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

‎problems/0027.移除元素.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,15 @@ Java:
165165
```java
166166
class Solution {
167167
public int removeElement(int[] nums, int val) {
168-
169168
// 快慢指针
170-
int fastIndex = 0;
171-
int slowIndex;
172-
for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
169+
int slowIndex = 0;
170+
for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
173171
if (nums[fastIndex] != val) {
174172
nums[slowIndex] = nums[fastIndex];
175173
slowIndex++;
176174
}
177175
}
178176
return slowIndex;
179-
180177
}
181178
}
182179
```

‎problems/0150.逆波兰表达式求值.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565

6666
# 思路
6767

68+
《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频在看本篇题解,更有助于大家对本题的理解。
69+
6870
在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。
6971

7072
所以**栈与递归之间在某种程度上是可以转换的!** 这一点我们在后续讲解二叉树的时候,会更详细的讲解到。

‎problems/0239.滑动窗口最大值.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
# 思路
3434

35+
《代码随想录》算法视频公开课:[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频在看本篇题解,更有助于大家对本题的理解。
36+
3537
这是使用单调队列的经典题目。
3638

3739
难点是如何求一个区间里的最大值呢? (这好像是废话),暴力一下不就得了。

‎problems/0347.前K个高频元素.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131

3232
# 思路
3333

34+
《代码随想录》算法视频公开课:[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频在看本篇题解,更有助于大家对本题的理解。
35+
36+
<p align="center">
37+
<iframe src="//player.bilibili.com/player.html?aid=514643371&bvid=BV1Xg41167Lz&cid=808260290&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width=750 height=500></iframe>
38+
</p>
39+
40+
41+
3442
这道题目主要涉及到如下三块内容:
3543
1. 要统计元素出现频率
3644
2. 对频率排序

‎problems/0695.岛屿的最大面积.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
# 思路
1818

19+
这道题目也是 dfs bfs基础类题目。
20+
21+
22+
## DFS
23+
24+
很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过!
25+
26+
这里其实涉及到dfs的两种写法,
27+
28+
以下代码使用dfs实现,如果对dfs不太了解的话,建议先看这篇题解:[797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/),
29+
1930
写法一,dfs只处理下一个节点
2031
```CPP
2132
class Solution {
@@ -94,3 +105,55 @@ public:
94105
}
95106
};
96107
```
108+
109+
以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。
110+
111+
## BFS
112+
113+
```CPP
114+
class Solution {
115+
private:
116+
int count;
117+
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
118+
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
119+
queue<int> que;
120+
que.push(x);
121+
que.push(y);
122+
visited[x][y] = true; // 加入队列就意味节点是陆地可到达的点
123+
count++;
124+
while(!que.empty()) {
125+
int xx = que.front();que.pop();
126+
int yy = que.front();que.pop();
127+
for (int i = 0 ;i < 4; i++) {
128+
int nextx = xx + dir[i][0];
129+
int nexty = yy + dir[i][1];
130+
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界
131+
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 节点没有被访问过且是陆地
132+
visited[nextx][nexty] = true;
133+
count++;
134+
que.push(nextx);
135+
que.push(nexty);
136+
}
137+
}
138+
}
139+
}
140+
141+
public:
142+
int maxAreaOfIsland(vector<vector<int>>& grid) {
143+
int n = grid.size(), m = grid[0].size();
144+
vector<vector<bool>> visited = vector<vector<bool>>(n, vector<bool>(m, false));
145+
int result = 0;
146+
for (int i = 0; i < n; i++) {
147+
for (int j = 0; j < m; j++) {
148+
if (!visited[i][j] && grid[i][j] == 1) {
149+
count = 0;
150+
bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
151+
result = max(result, count);
152+
}
153+
}
154+
}
155+
return result;
156+
}
157+
};
158+
159+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /