{"id":986,"date":"2025-12-16T06:49:23","date_gmt":"2025-12-15T22:49:23","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=986"},"modified":"2025-12-16T06:49:23","modified_gmt":"2025-12-15T22:49:23","slug":"leetcode-74-%e6%90%9c%e7%b4%a2%e4%ba%8c%e7%bb%b4%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=986","title":{"rendered":"LeetCode 74 &#8211; \u641c\u7d22\u4e8c\u7ef4\u77e9\u9635"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6ee1\u8db3\u4e0b\u8ff0\u4e24\u6761\u5c5e\u6027\u7684 m x n \u6574\u6570\u77e9\u9635\uff1a<br \/>\n\u6bcf\u884c\u4e2d\u7684\u6574\u6570\u4ece\u5de6\u5230\u53f3\u6309\u975e\u4e25\u683c\u9012\u589e\u987a\u5e8f\u6392\u5217\u3002<br \/>\n\u6bcf\u884c\u7684\u7b2c\u4e00\u4e2a\u6574\u6570\u5927\u4e8e\u524d\u4e00\u884c\u7684\u6700\u540e\u4e00\u4e2a\u6574\u6570\u3002<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 target \uff0c\u5982\u679c target \u5728\u77e9\u9635\u4e2d\uff0c\u8fd4\u56de true \uff1b\u5426\u5219\uff0c\u8fd4\u56de false \u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_637309c7f9095b31fc15d2d8b6ce236c.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_637309c7f9095b31fc15d2d8b6ce236c.jpg\" alt=\"\" \/><\/a><\/p>\n<p>\u8f93\u5165\uff1amatrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<br \/>\n\u8f93\u51fa\uff1atrue<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u4e8c\u5206\u67e5\u627e<\/h2>\n<p>\u6bcf\u884c\u4ece\u5de6\u5230\u53f3\u9012\u589e\uff0c\u540c\u65f6\u6bcf\u884c\u7b2c\u4e00\u4e2a\u6570\u5b57\u5927\u4e8e\u4e0a\u4e00\u884c\u6700\u540e\u4e00\u4e2a\u6570\u5b57\u3002<br \/>\n\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u6298\u53e0\u6210\u4e86\u4e8c\u7ef4\uff0c\u76f4\u63a5\u6309\u7167\u4e00\u7ef4\u7684\u6a21\u5f0f\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u5373\u53ef\u3002\u65f6\u95f4\u590d\u6742\u5ea6 O(log(m * n)).<\/p>\n<h2>\u4ece\u53f3\u4e0a\u89d2\u679a\u4e3e<\/h2>\n<p>\u5982\u679c\u6ca1\u6709\u3010\u6bcf\u884c\u7684\u7b2c\u4e00\u4e2a\u6574\u6570\u5927\u4e8e\u524d\u4e00\u884c\u7684\u6700\u540e\u4e00\u4e2a\u6574\u6570\u3011\u8fd9\u4e2a\u6761\u4ef6\uff0c\u4ec5\u4ec5\u662f\u6bcf\u884c\u9012\u589e \u6bcf\u5217\u4e5f\u9012\u589e\uff0c\u5c31\u4e0d\u592a\u65b9\u4fbf\u4e8c\u5206\u67e5\u627e\u4e86\u3002<br \/>\n\u4ece\u5de6\u4e0a\u89d2\u51fa\u53d1\uff0c\u4f1a\u53d1\u73b0\u6bcf\u6b21\u90fd\u9762\u4e342\u4e2a\u9009\u62e9\uff0c\u5411\u53f3\u6216\u5411\u4e0b\u3002<br \/>\n\u800c\u4ece\u53f3\u4e0a\u89d2\u51fa\u53d1\uff0c\u5219\u6bcf\u6b21\u53ea\u9762\u4e34\u4e00\u4e2a\u9009\u62e9\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\uff0c\u6216\u8df3\u51fa\u7f51\u683c\u8fb9\u754c\u3002\u65f6\u95f4\u590d\u6742\u5ea6 O(m + n).<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public boolean searchMatrix(int[][] matrix, int target) {\n    int m = matrix.length, n = matrix[0].length;\n    int size = m * n;\n    int left = 0, right = size - 1, mid = 0;\n    while (left &lt;= right) {\n        mid = (left + right) \/ 2;\n        int cow = mid \/ n;\n        int row = mid % n;\n        if (matrix[cow][row] &lt; target) {\n            left = mid + 1;\n        } else if (matrix[cow][row] &gt; target) {\n            right = mid - 1;\n        } else {\n            return true;\n        }\n    }\n    return false;\n}\n\npublic boolean searchMatrix2(int[][] matrix, int target) {\n    int m = matrix.length, n = matrix[0].length;\n    int i = 0, j = n - 1;\n    while (i &gt;= 0 &amp;&amp; i &lt; m &amp;&amp; j &gt;= 0 &amp;&amp; j &lt; n) {\n        if (matrix[i][j] &gt; target) {\n            j--;\n        } else if (matrix[i][j] &lt; target) {\n            i++;\n        } else {\n            return true;\n        }\n    }\n    return false;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun searchMatrix(matrix: Array&lt;IntArray&gt;, target: Int): Boolean {\n    val m = matrix.size\n    val n = matrix[0].size\n    val size = m * n\n    var left = 0\n    var right = size - 1\n    while (left &lt;= right) {\n        val mid = (left + right) \/ 2\n        val cow = mid \/ n\n        val row = mid % n\n        if (matrix[cow][row] &lt; target) {\n            left = mid + 1\n        } else if (matrix[cow][row] &gt; target) {\n            right = mid - 1\n        } else {\n            return true\n        }\n    }\n    return false\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6ee1\u8db3\u4e0b\u8ff0\u4e24\u6761\u5c5e\u6027\u7684 m x n \u6574\u6570\u77e9\u9635\uff1a \u6bcf\u884c\u4e2d\u7684\u6574\u6570\u4ece\u5de6\u5230\u53f3\u6309\u975e\u4e25\u683c\u9012\u589e\u987a\u5e8f\u6392\u5217\u3002 \u6bcf\u884c [&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,32,83],"class_list":["post-986","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-32","tag-83"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/986","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=986"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions"}],"predecessor-version":[{"id":987,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions\/987"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}