Skip to content

Commit 8305608

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents c4a3fe0 + 3c1b7b7 commit 8305608

35 files changed

+427
-47
lines changed

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -372,25 +372,38 @@
372372

373373
通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor
374374

375-
### 深搜广搜
376-
377-
* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md)
378-
* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md)
379-
* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md)
380-
* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md)
381-
* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md)
382-
* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md)
383-
* [图论:1020.飞地的数量](./problems/1020.飞地的数量.md)
384-
* [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md)
385-
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
386-
* [图论:827.最大人工岛](./problems/0827.最大人工岛.md)
387-
* [图论:127. 单词接龙](./problems/0127.单词接龙.md)
388-
* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md)
389-
* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md)
390-
* [图论:并查集理论基础](./problems/图论并查集理论基础.md)
391-
* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
392-
* [图论:684.冗余连接](./problems/0684.冗余连接.md)
393-
* [图论:685.冗余连接II](./problems/0685.冗余连接II.md)
375+
1. [图论:理论基础](./problems/kamacoder/图论理论基础.md)
376+
2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md)
377+
3. [图论:所有可达路径](./problems/kamacoder/0098.所有可达路径.md)
378+
4. [图论:广度优先搜索理论基础](./problems/kamacoder/图论广搜理论基础.md)
379+
5. [图论:岛屿数量.深搜版](./problems/kamacoder/0099.岛屿的数量深搜.md)
380+
6. [图论:岛屿数量.广搜版](./problems/kamacoder/0099.岛屿的数量广搜.md)
381+
7. [图论:岛屿的最大面积](./problems/kamacoder/0100.岛屿的最大面积.md)
382+
8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md)
383+
9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md)
384+
10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md)
385+
11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md)
386+
12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md)
387+
13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md)
388+
14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md)
389+
15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md)
390+
16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md)
391+
17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md)
392+
18. [图论:冗余连接II](./problems/kamacoder/0109.冗余连接II.md)
393+
19. [图论:最小生成树之prim](./problems/kamacoder/0053.寻宝-prim.md)
394+
20. [图论:最小生成树之kruskal](./problems/kamacoder/0053.寻宝-Kruskal.md)
395+
21. [图论:拓扑排序](./problems/kamacoder/0117.软件构建.md)
396+
22. [图论:dijkstra(朴素版)](./problems/kamacoder/0047.参会dijkstra朴素.md)
397+
23. [图论:dijkstra(堆优化版)](./problems/kamacoder/0047.参会dijkstra堆.md)
398+
24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md)
399+
25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md)
400+
26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md)
401+
27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0095.城市间货物运输II.md)
402+
28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md)
403+
29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md)
404+
30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md)
405+
31. [图论:图论总结篇](./problems/kamacoder/图论总结篇.md)
406+
394407

395408
(持续更新中....)
396409

problems/0417.太平洋大西洋水流问题.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ public:
177177
178178
// 记录从大西洋出发,可以遍历的节点
179179
vector<vector<bool>> atlantic = vector<vector<bool>>(n, vector<bool>(m, false));
180-
181-
// 从最上最下行的节点出发,向高处遍历
180+
181+
// 从最左最右列的节点出发,向高处遍历
182182
for (int i = 0; i < n; i++) {
183183
dfs (heights, pacific, i, 0); // 遍历最左列,接触太平洋
184184
dfs (heights, atlantic, i, m - 1); // 遍历最右列,接触大西
185185
}
186186
187-
// 从最左最右列的节点出发,向高处遍历
187+
// 从最上最下行的节点出发,向高处遍历
188188
for (int j = 0; j < m; j++) {
189189
dfs (heights, pacific, 0, j); // 遍历最上行,接触太平洋
190190
dfs (heights, atlantic, n - 1, j); // 遍历最下行,接触大西洋
@@ -297,6 +297,73 @@ class Solution {
297297
}
298298
```
299299

300+
```Java
301+
class Solution {
302+
303+
// 和Carl题解更加符合的Java DFS
304+
private int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
305+
306+
/**
307+
* @param heights 题目给定的二维数组
308+
* @param m 当前位置的行号
309+
* @param n 当前位置的列号
310+
* @param visited 记录这个位置可以到哪条河
311+
*/
312+
313+
public void dfs(int[][] heights, boolean[][] visited, int m, int n){
314+
if(visited[m][n]) return;
315+
visited[m][n] = true;
316+
317+
for(int[] dir: directions){
318+
int nextm = m + dir[0];
319+
int nextn = n + dir[1];
320+
//出了2D array的边界,continue
321+
if(nextm < 0||nextm == heights.length||nextn <0||nextn== heights[0].length) continue;
322+
//下一个位置比当下位置还要低,跳过,继续找下一个更高的位置
323+
if(heights[m][n] > heights[nextm][nextn]) continue;
324+
dfs(heights, visited, nextm, nextn);
325+
}
326+
}
327+
328+
329+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
330+
int m = heights.length;
331+
int n = heights[0].length;
332+
333+
// 记录从太平洋边出发,可以遍历的节点
334+
boolean[][] pacific = new boolean[m][n];
335+
// 记录从大西洋出发,可以遍历的节点
336+
boolean[][] atlantic = new boolean[m][n];
337+
338+
// 从最左最右列的节点出发,向高处遍历
339+
for(int i = 0; i<m; i++){
340+
dfs(heights, pacific, i, 0); //遍历pacific最左边
341+
dfs(heights, atlantic, i, n-1); //遍历atlantic最右边
342+
}
343+
344+
// 从最上最下行的节点出发,向高处遍历
345+
for(int j = 0; j<n; j++){
346+
dfs(heights, pacific, 0, j); //遍历pacific最上边
347+
dfs(heights, atlantic, m-1, j); //遍历atlantic最下边
348+
}
349+
350+
List<List<Integer>> result = new ArrayList<>();
351+
for(int a = 0; a<m; a++){
352+
for(int b = 0; b<n; b++){
353+
// 如果这个节点,从太平洋和大西洋出发都遍历过,就是结果
354+
if(pacific[a][b] && atlantic[a][b]){
355+
List<Integer> pair = new ArrayList<>();
356+
pair.add(a);
357+
pair.add(b);
358+
result.add(pair);
359+
}
360+
}
361+
}
362+
return result;
363+
}
364+
}
365+
```
366+
300367
广度优先遍历:
301368

302369
```Java

problems/0474.一和零.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,89 @@ public:
159159
* 时间复杂度: O(kmn),k 为strs的长度
160160
* 空间复杂度: O(mn)
161161
162+
C++:
163+
使用三维数组的版本
162164
165+
```CPP
166+
class Solution {
167+
public:
168+
int findMaxForm(vector<string>& strs, int m, int n) {
169+
int num_of_str = strs.size();
170+
171+
vector<vector<vector<int>>> dp(num_of_str, vector<vector<int>>(m + 1,vector<int>(n + 1, 0)));
172+
173+
/* dp[i][j][k] represents, if choosing items among strs[0] to strs[i] to form a subset,
174+
what is the maximum size of this subset such that there are no more than m 0's and n 1's in this subset.
175+
Each entry of dp[i][j][k] is initialized with 0
176+
177+
transition formula:
178+
using x[i] to indicates the number of 0's in strs[i]
179+
using y[i] to indicates the number of 1's in strs[i]
180+
181+
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - x[i]][k - y[i]] + 1)
182+
183+
*/
184+
185+
186+
// num_of_zeros records the number of 0's for each str
187+
// num_of_ones records the number of 1's for each str
188+
// find the number of 0's and the number of 1's for each str in strs
189+
vector<int> num_of_zeros;
190+
vector<int> num_of_ones;
191+
for (auto& str : strs){
192+
int count_of_zero = 0;
193+
int count_of_one = 0;
194+
for (char &c : str){
195+
if(c == '0') count_of_zero ++;
196+
else count_of_one ++;
197+
}
198+
num_of_zeros.push_back(count_of_zero);
199+
num_of_ones.push_back(count_of_one);
200+
201+
}
202+
203+
204+
// num_of_zeros[0] indicates the number of 0's for str[0]
205+
// num_of_ones[0] indiates the number of 1's for str[1]
206+
207+
// initialize the 1st plane of dp[i][j][k], i.e., dp[0][j][k]
208+
// if num_of_zeros[0] > m or num_of_ones[0] > n, no need to further initialize dp[0][j][k],
209+
// because they have been intialized to 0 previously
210+
if(num_of_zeros[0] <= m && num_of_ones[0] <= n){
211+
// for j < num_of_zeros[0] or k < num_of_ones[0], dp[0][j][k] = 0
212+
for(int j = num_of_zeros[0]; j <= m; j++){
213+
for(int k = num_of_ones[0]; k <= n; k++){
214+
dp[0][j][k] = 1;
215+
}
216+
}
217+
}
218+
219+
/* if j - num_of_zeros[i] >= 0 and k - num_of_ones[i] >= 0:
220+
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - num_of_zeros[i]][k - num_of_ones[i]] + 1)
221+
else:
222+
dp[i][j][k] = dp[i-1][j][k]
223+
*/
224+
225+
for (int i = 1; i < num_of_str; i++){
226+
int count_of_zeros = num_of_zeros[i];
227+
int count_of_ones = num_of_ones[i];
228+
for (int j = 0; j <= m; j++){
229+
for (int k = 0; k <= n; k++){
230+
if( j < count_of_zeros || k < count_of_ones){
231+
dp[i][j][k] = dp[i-1][j][k];
232+
}else{
233+
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - count_of_zeros][k - count_of_ones] + 1);
234+
}
235+
}
236+
}
237+
238+
}
239+
240+
return dp[num_of_str-1][m][n];
241+
242+
}
243+
};
244+
```
163245

164246
## 总结
165247

problems/kamacoder/0047.参会dijkstra堆.md

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

2+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
3+
24
# dijkstra(堆优化版)精讲
35

46
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)

problems/kamacoder/0047.参会dijkstra朴素.md

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

2+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
3+
24
# dijkstra(朴素版)精讲
35

46
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)

problems/kamacoder/0053.寻宝-Kruskal.md

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

2+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
3+
24
# kruskal算法精讲
35

46
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)

problems/kamacoder/0053.寻宝-prim.md

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

2+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
3+
24
# prim算法精讲
35

46
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)

problems/kamacoder/0054.替换数字.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11

2-
<p align="center">
3-
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
4-
<img src="../pics/训练营.png" width="1000"/>
5-
</a>
6-
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
7-
8-
92
# 替换数字
103

114
[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064)
@@ -376,9 +369,3 @@ main();
376369
### Rust:
377370

378371

379-
380-
<p align="center">
381-
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
382-
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
383-
</a>
384-

problems/kamacoder/0055.右旋字符串.md

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

2-
<p align="center">
3-
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
4-
<img src="../pics/训练营.png" width="1000"/>
5-
</a>
6-
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
72

83

94
# 右旋字符串
@@ -350,9 +345,3 @@ var reverseLeftWords = function(s, n) {
350345
### Rust:
351346

352347

353-
354-
355-
<p align="center">
356-
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
357-
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
358-
</a>

problems/kamacoder/0094.城市间货物运输I-SPFA.md

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

2+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
3+
24
# Bellman_ford 队列优化算法(又名SPFA)
35

46
[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152)

0 commit comments

Comments
 (0)