一文秒杀所有岛屿题目 :: labuladong的算法小抄 #1065
Replies: 60 comments 10 replies
-
第二题那个 不是会把第二层的那些也变成1吗 但是没有变 搞不清楚了 大佬可以解答一些吗 |
Beta Was this translation helpful? Give feedback.
-
走0,1的时候 1,0为0也为变为1啊 这个代码 有些看不懂了 第二题 |
Beta Was this translation helpful? Give feedback.
-
上面那个老哥,你仔细看一下代码,如果这个格子是水的话就直接return不会往旁边淹,如果是陆地的话当然要往旁边淹,因为这个格子+旁边的陆地形成的就是靠边的岛屿。 |
Beta Was this translation helpful? Give feedback.
-
你打印一下 坐标就知道了 有一个是1.1 但是1.1肯定不是啊 |
Beta Was this translation helpful? Give feedback.
-
如果这题的0.1的值是0的话 结果就变成1个了 |
Beta Was this translation helpful? Give feedback.
-
0,1的值如果是0,那结果肯定是1了啊,那就少了中间那一片了嘛,因为去掉边界岛屿的时候,都会变为水。去掉边界的岛屿,不是去掉边界上的那 一个 陆地格子,所以,相邻的都是岛屿。我猜你可能是这里想错了 |
Beta Was this translation helpful? Give feedback.
-
封闭岛屿的数量,一来就把四周最外层的变成1,本来靠边有好几个0,你单独把最外层的变成1了,那其他的0会不会被1(由四周的0变成的1)包围,形成独岛呢? |
Beta Was this translation helpful? Give feedback.
-
不是最外层变为1,是靠边的岛屿变为海水,那就不会有独岛了啊 |
Beta Was this translation helpful? Give feedback.
-
至于为什么初始调用 dfs 函数时的 dir 参数可以随意写? |
Beta Was this translation helpful? Give feedback.
-
@Days-Go-By 明白了,会把边界的四个方向的邻居全部变成海,所以不会产生独岛。 |
Beta Was this translation helpful? Give feedback.
-
@huangpan2507 你是说哪里不能跳转? |
Beta Was this translation helpful? Give feedback.
-
最前面讲dfs遍历二维数组的代码中visited是boolean[][]二维数组吧,一维的boolean[]报错。 |
Beta Was this translation helpful? Give feedback.
-
我是这样解答子岛屿问题的(C#,运行时间比100%的提交短(力扣英文版))
思路是将dfs进行改造。 |
Beta Was this translation helpful? Give feedback.
-
这解法喵喵喵,一大早看过算法题,一天轻飘飘 |
Beta Was this translation helpful? Give feedback.
-
@savvy1st 哈哈,人才😂 |
Beta Was this translation helpful? Give feedback.
-
哎,这些算法真的是很巧妙呢。 |
Beta Was this translation helpful? Give feedback.
-
打卡,打卡。 种类去重用hash table 记录已经有的岛屿形状,hash table 的key是岛屿形状的序列化 |
Beta Was this translation helpful? Give feedback.
-
东哥,你最后一道题顺序的comment是不是写反了,按顺序来应该是左 右 上 下吧 |
Beta Was this translation helpful? Give feedback.
-
如果是不同岛屿的数量II, 岛屿可以翻转呢 |
Beta Was this translation helpful? Give feedback.
-
DFS和回溯区别对于最后一题不同的岛屿数量,最后提到 根据东哥图论算法基础提到的二者区别,给出下面我自己的理解。 东哥用的是DFS,关注在节点。 int[][] dir = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int[][] grid, int i, int j, StringBuilder sb) {
int m = grid.length, n = grid[0].length;
if (i < 0 || j < 0 || i >= m || j >= n
|| grid[i][j] == 0) {
return;
}
grid[i][j] = 0;
for(int k = 1; k <= 4; k++){
int ni = i + dir[k - 1][0];
int nj = j + dir[k - 1][1];
// 前序遍历位置:进入 (ni, nj)
sb.append(k).append(',');
dfs(grid, ni, nj, sb);
// 后序遍历位置:离开 (ni, nj)
sb.append(-k).append(',');
}
} 两者区别可以用下图表示 从这里也可以看出为什么东哥说初始调用 |
Beta Was this translation helpful? Give feedback.
-
floodfill的思路太有意思了,最后的序列化也很亮,nice |
Beta Was this translation helpful? Give feedback.
-
感谢大佬的岛屿框架呜呜呜! |
Beta Was this translation helpful? Give feedback.
-
东哥,在1254,统计封闭岛屿的数目里面,把靠边的岛屿淹没之后,在遍历grid获取封闭岛屿数目的时候,可以不用遍历grid的最外层了,一点点小建议 |
Beta Was this translation helpful? Give feedback.
-
越看越觉得东哥牛逼 |
Beta Was this translation helpful? Give feedback.
-
子岛屿,为什么不可以用 grid1[r][c]!=grid2[r][c] 去判断一定不是子岛屿? |
Beta Was this translation helpful? Give feedback.
-
讲得通俗易懂,效率起飞 |
Beta Was this translation helpful? Give feedback.
-
最后一题不加后续遍历位置是不是也可以?也能区分唯一性? // 后序遍历位置:离开 (i, j) |
Beta Was this translation helpful? Give feedback.
-
其实不记录撤销也是可以的 - 但如果不记录撤销的话,进入的路径连着0必须一起记录;如果不记录0的路径只记录1,那必须要记录撤销,不然像东哥说的这种情况
1和0都记录且不记录撤销的话,两个岛屿路径分别是: 如果只记录1且不记录撤销的话,两个岛屿路径就都是: 以下是我的记录进入0与1的路径不记录撤销且通过的python代码。写成了VSCode的形式,方便debug def numDistinctIslands(grid) -> int:
|
Beta Was this translation helpful? Give feedback.
-
文章链接点这里:一文秒杀所有岛屿题目
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions