diff --git "a/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" "b/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" index 12f88c75df..05c4394cab 100644 --- "a/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" +++ "b/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" @@ -165,18 +165,15 @@ Java: ```java class Solution { public int removeElement(int[] nums, int val) { - // 快慢指针 - int fastIndex = 0; - int slowIndex; - for (slowIndex = 0; fastIndex < nums.length; fastIndex++) { + int slowIndex = 0; + for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) { if (nums[fastIndex] != val) { nums[slowIndex] = nums[fastIndex]; slowIndex++; } } return slowIndex; - } } ``` diff --git "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" index 21bd85935f..8107e4e07a 100644 --- "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" +++ "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" @@ -65,6 +65,8 @@ # 思路 +《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频在看本篇题解,更有助于大家对本题的理解。 + 在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。 所以**栈与递归之间在某种程度上是可以转换的!** 这一点我们在后续讲解二叉树的时候,会更详细的讲解到。 diff --git "a/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" "b/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" index 6ec985069f..3f402f1203 100644 --- "a/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" +++ "b/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" @@ -32,6 +32,8 @@ # 思路 +《代码随想录》算法视频公开课:[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频在看本篇题解,更有助于大家对本题的理解。 + 这是使用单调队列的经典题目。 难点是如何求一个区间里的最大值呢? (这好像是废话),暴力一下不就得了。 diff --git "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" index 430dcea1d8..d4059b9ba4 100644 --- "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" +++ "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" @@ -31,6 +31,14 @@ # 思路 +《代码随想录》算法视频公开课:[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频在看本篇题解,更有助于大家对本题的理解。 + +

+[フレーム] +

+ + + 这道题目主要涉及到如下三块内容: 1. 要统计元素出现频率 2. 对频率排序 diff --git "a/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" "b/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" index b09452921f..3739882aff 100644 --- "a/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" +++ "b/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" @@ -16,6 +16,17 @@ # 思路 +这道题目也是 dfs bfs基础类题目。 + + +## DFS + +很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过! + +这里其实涉及到dfs的两种写法, + +以下代码使用dfs实现,如果对dfs不太了解的话,建议先看这篇题解:[797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/), + 写法一,dfs只处理下一个节点 ```CPP class Solution { @@ -94,3 +105,55 @@ public: } }; ``` + +以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。 + +## BFS + +```CPP +class Solution { +private: + int count; + int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 + void bfs(vector>& grid, vector>& visited, int x, int y) { + queue que; + que.push(x); + que.push(y); + visited[x][y] = true; // 加入队列就意味节点是陆地可到达的点 + count++; + while(!que.empty()) { + int xx = que.front();que.pop(); + int yy = que.front();que.pop(); + for (int i = 0 ;i < 4; i++) { + int nextx = xx + dir[i][0]; + int nexty = yy + dir[i][1]; + if (nextx < 0 || nextx>= grid.size() || nexty < 0 || nexty>= grid[0].size()) continue; // 越界 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 节点没有被访问过且是陆地 + visited[nextx][nexty] = true; + count++; + que.push(nextx); + que.push(nexty); + } + } + } + } + +public: + int maxAreaOfIsland(vector>& grid) { + int n = grid.size(), m = grid[0].size(); + vector> visited = vector>(n, vector(m, false)); + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + count = 0; + bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + result = max(result, count); + } + } + } + return result; + } +}; + +```

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