{"id":959,"date":"2025-12-14T04:40:38","date_gmt":"2025-12-13T20:40:38","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=959"},"modified":"2025-12-14T04:40:38","modified_gmt":"2025-12-13T20:40:38","slug":"leetcode-200-%e5%b2%9b%e5%b1%bf%e6%95%b0%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=959","title":{"rendered":"LeetCode 200 &#8211; \u5c9b\u5c7f\u6570\u91cf"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u7531 &#8216;1&#8217;\uff08\u9646\u5730\uff09\u548c &#8216;0&#8217;\uff08\u6c34\uff09\u7ec4\u6210\u7684\u7684\u4e8c\u7ef4\u7f51\u683c\uff0c\u8bf7\u4f60\u8ba1\u7b97\u7f51\u683c\u4e2d\u5c9b\u5c7f\u7684\u6570\u91cf\u3002<br \/>\n\u5c9b\u5c7f\u603b\u662f\u88ab\u6c34\u5305\u56f4\uff0c\u5e76\u4e14\u6bcf\u5ea7\u5c9b\u5c7f\u53ea\u80fd\u7531\u6c34\u5e73\u65b9\u5411\u548c\/\u6216\u7ad6\u76f4\u65b9\u5411\u4e0a\u76f8\u90bb\u7684\u9646\u5730\u8fde\u63a5\u5f62\u6210\u3002<br \/>\n\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u5047\u8bbe\u8be5\u7f51\u683c\u7684\u56db\u6761\u8fb9\u5747\u88ab\u6c34\u5305\u56f4\u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u4f7f\u7528\u6570\u7ec4 visited \u6807\u8bb0\u88ab\u8bbf\u95ee\u8fc7\u7684\u70b9\u3002<br \/>\n\u904d\u5386grid\uff0c\u503c\u4e3a 1 \u4e14\u6ca1\u6709\u88ab\u8bbf\u95ee\u8fc7\uff0c\u5219\u8ba4\u4e3a\u662f\u65b0\u5c9b\u5c7f\uff0c\u8c03\u7528 DFS \u5bf9\u5c9b\u5c7f\u5185\u7684\u6240\u6709\u9646\u5730\u505a\u6807\u8bb0\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">boolean[][] visited;\nchar[][] grid;\nint m, n;\n\npublic int numIslands(char[][] grid) {\n    this.grid = grid;\n    m = grid.length;\n    n = grid[0].length;\n    int count = 0;\n    visited = new boolean[m][n];\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (grid[i][j] == '1' &amp;&amp; !visited[i][j]) {\n                count++;\n                dfs(i, j);\n            }\n        }\n    }\n    return count;\n}\n\nprivate void dfs(int i, int j) {\n    if (i &lt; 0 || i &gt;= m || j &lt; 0 || j &gt;= n || visited[i][j] || grid[i][j] != '1') {\n        return;\n    }\n    visited[i][j] = true;\n    dfs(i, j + 1);\n    dfs(i, j - 1);\n    dfs(i + 1, j);\n    dfs(i - 1, j);\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private lateinit var grid: Array&lt;CharArray&gt;\nprivate lateinit var visited: Array&lt;BooleanArray&gt;\n\nfun numIslands(grid: Array&lt;CharArray&gt;): Int {\n    this.grid = grid\n    visited = Array(grid.size) { BooleanArray(grid[0].size) }\n    var count = 0\n    for (i in grid.indices) {\n        for (j in grid[0].indices) {\n            if (grid[i][j] == '1' &amp;&amp; !visited[i][j]) {\n                dfs(i, j)\n                count++\n            }\n        }\n    }\n    return count\n}\n\nprivate fun dfs(i: Int, j: Int) {\n    if (i !in grid.indices || j !in grid[0].indices || visited[i][j] || grid[i][j] != '1') {\n        return\n    }\n    visited[i][j] = true\n    dfs(i, j + 1)\n    dfs(i, j - 1)\n    dfs(i - 1, j)\n    dfs(i + 1, j)\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u7531 &#8216;1&#8217;\uff08\u9646\u5730\uff09\u548c &#8216;0&#8217;\uff08\u6c34\uff09\u7ec4\u6210\u7684\u7684\u4e8c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,73],"class_list":["post-959","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-73"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=959"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":960,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/959\/revisions\/960"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}