{"id":1066,"date":"2026-01-02T12:36:42","date_gmt":"2026-01-02T04:36:42","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1066"},"modified":"2026-01-02T12:36:42","modified_gmt":"2026-01-02T04:36:42","slug":"leetcode-134-%e5%8a%a0%e6%b2%b9%e7%ab%99","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1066","title":{"rendered":"LeetCode 134 \u2013 \u52a0\u6cb9\u7ad9"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709 n \u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9 gas[i] \u5347\u3002<\/p>\n<p>\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\u4f60\u4ece\u5176\u4e2d\u7684\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u5f00\u59cb\u65f6\u6cb9\u7bb1\u4e3a\u7a7a\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\u6570\u7ec4 gas \u548c cost \uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u6309\u987a\u5e8f\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\uff0c\u5219\u8fd4\u56de\u51fa\u53d1\u65f6\u52a0\u6cb9\u7ad9\u7684\u7f16\u53f7\uff0c\u5426\u5219\u8fd4\u56de -1 \u3002\u5982\u679c\u5b58\u5728\u89e3\uff0c\u5219 \u4fdd\u8bc1 \u5b83\u662f \u552f\u4e00 \u7684\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165: gas = [1,2,3,4,5], cost = [3,4,5,1,2]<br \/>\n\u8f93\u51fa: 3<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u8d2a\u5fc3\u3002<br \/>\n\u5f53\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u53ef\u4ee5\u79fb\u52a8\u5230 i + 1 \u65f6\uff0c\u8bf4\u660e i + 1 \u4e00\u5b9a\u4e0d\u662f\u8d77\u70b9\uff08\u89e3\u552f\u4e00\uff09\uff0c\u5373\u4f7f\u540e\u9762\u53d1\u73b0\u4ece i \u51fa\u53d1\u4e0d\u80fd\u73af\u7ed5\u4e00\u5468\uff0c\u4e5f\u65e0\u9700\u4ece i + 1 \u5f00\u59cb\u51fa\u53d1\uff0c\u76f4\u63a5\u4ece i + x \u540e\u51fa\u53d1\u3002<br \/>\n\u4ece\u7b2c\u4e00\u4e2a\u5f00\u59cb\u51fa\u53d1\uff0c\u7ef4\u62a4\u603b\u6cb9\u91cf sum\uff0csum &lt; 0 \u65f6\u91cd\u7f6e\u8d77\u59cb\u70b9 start\u3002<br \/>\n\u76f4\u5230 i = start + length \uff08\u6210\u529f\uff09\uff0c\u6216 i = 2 * length \uff08\u5931\u8d25\uff09\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int canCompleteCircuit(int[] gas, int[] cost) {\n    int sum = 0;\n    int start = Integer.MIN_VALUE;\n    for (int i = 0; i &lt; 2 * gas.length; i++) {\n        if (i == start + gas.length) {\n            return start;\n        }\n        int index = i % gas.length;\n        sum += gas[index] - cost[index];\n        if (sum &gt;= 0) {\n            if (start == Integer.MIN_VALUE) {\n                start = i;\n            }\n        } else {\n            start = Integer.MIN_VALUE;\n            sum = 0;\n        }\n    }\n    return -1;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {\n    var sum = 0\n    val length = gas.size\n    var start = Int.MIN_VALUE\n    for (i in 0 until 2 * length) {\n        if (i == start + length) {\n            return start\n        }\n        val index = i % length\n        sum += gas[index] - cost[index]\n        if (sum &gt;= 0) {\n            if (start == Int.MIN_VALUE) {\n                start = i\n            }\n        } else {\n            start = Int.MIN_VALUE\n            sum = 0\n        }\n    }\n    return -1\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709 n \u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9 gas[i] \u5347\u3002 \u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684 [&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,62],"class_list":["post-1066","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-62"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1066","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=1066"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1066\/revisions"}],"predecessor-version":[{"id":1067,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1066\/revisions\/1067"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}