{"id":902,"date":"2025-10-24T23:54:12","date_gmt":"2025-10-24T15:54:12","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=902"},"modified":"2025-10-24T23:54:12","modified_gmt":"2025-10-24T15:54:12","slug":"leetcode-73-%e7%9f%a9%e9%98%b5%e7%bd%ae%e9%9b%b6","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=902","title":{"rendered":"LeetCode 73 \u2013 \u77e9\u9635\u7f6e\u96f6"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a m x n \u7684\u77e9\u9635\uff0c\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u4e3a 0 \uff0c\u5219\u5c06\u5176\u6240\u5728\u884c\u548c\u5217\u7684\u6240\u6709\u5143\u7d20\u90fd\u8bbe\u4e3a 0 \u3002\u8bf7\u4f7f\u7528 \u539f\u5730 \u7b97\u6cd5\u3002<\/p>\n<p>\u793a\u4f8b 1\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_cd11a32e18516ff5c3f7913340819698.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_cd11a32e18516ff5c3f7913340819698.jpg\" alt=\"\" \/><\/a><br \/>\n\u8f93\u5165\uff1amatrix = [[1,1,1],[1,0,1],[1,1,1]]<br \/>\n\u8f93\u51fa\uff1a[[1,0,1],[0,0,0],[1,0,1]]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u5f00\u8f9f\u4e24\u4e2a\u6570\u7ec4\uff0c\u7528\u4e8e\u8bb0\u5f55\u54ea\u4e00\u884c\u67090\uff0c\u54ea\u4e00\u5217\u67090<br \/>\n\u518d\u679a\u4e3e\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u5b83\u7684\u884c\u548c\u5217\u662f\u5426\u66fe\u88ab\u8bb0\u5f55\u67090\uff0c\u5982\u679c\u662f\uff0c\u5219\u628a\u5f53\u524d\u5143\u7d20\u4fee\u6539\u4e3a0<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(m + n)<\/p>\n<p>\u7a7a\u95f4\u4e0a\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u4ee5\u6bcf\u4e00\u884c\/\u5217\u7b2c\u4e00\u4e2a\u5143\u7d20\u6765\u8bb0\u5f55\u8fd9\u884c\/\u5217\u6709\u6ca1\u67090<br \/>\n\u4f46\u7b2c\u4e00\u884c\u548c\u7b2c\u4e00\u5217\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u540c\u4e00\u4e2a\uff0c\u4f1a\u4ea7\u751f\u51b2\u7a81\uff0c\u6240\u4ee5\u7b2c\u4e00\u884c\u548c\u7b2c\u4e00\u5217\u5355\u72ec\u5f00\u4e2a\u53d8\u91cf\u6765\u8bb0\u5f55\u662f\u5426\u67090<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(1)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public void setZeroes1(int[][] matrix) {\n    int m = matrix.length;\n    int n = matrix[0].length;\n    boolean[] zeroX = new boolean[m];\n    boolean[] zeroY = new boolean[n];\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (matrix[i][j] == 0) {\n                zeroX[i] = true;\n                zeroY[j] = true;\n            }\n        }\n    }\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (zeroX[i] || zeroY[j]) {\n                matrix[i][j] = 0;\n            }\n        }\n    }\n}\n\npublic void setZeroes2(int[][] matrix) {\n    int m = matrix.length;\n    int n = matrix[0].length;\n    boolean zeroX0 = false, zeroY0 = false;\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (matrix[i][j] == 0) {\n                if (i == 0) {\n                    zeroX0 = true;\n                }\n                if (j == 0) {\n                    zeroY0 = true;\n                }\n                if (i != 0 &amp;&amp; j != 0) {\n                    matrix[i][0] = 0;\n                    matrix[0][j] = 0;\n                }\n            }\n        }\n    }\n\n    \/\/ \u5148\u5904\u7406\u540e\u9762\u7684\uff0c\u9632\u6b62\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\u88ab\u6c61\u67d3\n    for (int i = 1; i &lt; m; i++) {\n        for (int j = 1; j &lt; n; j++) {\n            if (matrix[i][0] == 0 || matrix[0][j] == 0) {\n                matrix[i][j] = 0;\n            }\n        }\n    }\n    \/\/ \u5904\u7406\u7b2c\u4e00\u5217\n    for (int i = 0; i &lt; m; i++) {\n        if (zeroY0) {\n            matrix[i][0] = 0;\n        }\n    }\n    \/\/ \u5904\u7406\u7b2c\u4e00\u884c\n    for (int j = 0; j &lt; n; j++) {\n        if (zeroX0) {\n            matrix[0][j] = 0;\n        }\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun setZeroes1(matrix: Array&lt;IntArray&gt;): Unit {\n    val m = matrix.size\n    val n = matrix[0].size\n    val zeroX = BooleanArray(m)\n    val zeroY = BooleanArray(n)\n    for (i in 0 until m) {\n        for (j in 0 until n) {\n            if (matrix[i][j] == 0) {\n                zeroX[i] = true\n                zeroY[j] = true\n            }\n        }\n    }\n    for (i in 0 until m) {\n        for (j in 0 until n) {\n            if (zeroX[i] || zeroY[j]) {\n                matrix[i][j] = 0\n            }\n        }\n    }\n}\n\nfun setZeroes2(matrix: Array&lt;IntArray&gt;): Unit {\n    val m = matrix.size\n    val n = matrix[0].size\n    var zeroX0 = false\n    var zeroY0 = false\n    for (i in 0 until m) {\n        for (j in 0 until n) {\n            if (matrix[i][j] == 0) {\n                if (i == 0) {\n                    zeroX0 = true\n                }\n                if (j == 0) {\n                    zeroY0 = true\n                }\n                if (i &gt; 0 &amp;&amp; j &gt; 0) {\n                    matrix[i][0] = 0\n                    matrix[0][j] = 0\n                }\n            }\n        }\n    }\n    for (i in 1 until m) {\n        for (j in 1 until n) {\n            if (matrix[i][0] == 0 || matrix[0][j] == 0) {\n                matrix[i][j] = 0\n            }\n        }\n    }\n    for (i in 0 until m) {\n        if (zeroY0) {\n            matrix[i][0] = 0\n        }\n    }\n    for (j in 0 until n) {\n        if (zeroX0) {\n            matrix[0][j] = 0\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a m x n \u7684\u77e9\u9635\uff0c\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u4e3a 0 \uff0c\u5219\u5c06\u5176\u6240\u5728\u884c\u548c\u5217\u7684\u6240\u6709\u5143\u7d20\u90fd\u8bbe\u4e3a 0 \u3002\u8bf7\u4f7f\u7528  [&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":[20,74,73,83],"class_list":["post-902","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-74","tag-73","tag-83"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/902","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=902"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":903,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/903"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}