{"id":999,"date":"2025-12-18T01:05:52","date_gmt":"2025-12-17T17:05:52","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=999"},"modified":"2025-12-18T01:05:52","modified_gmt":"2025-12-17T17:05:52","slug":"leetcode-118-%e6%9d%a8%e8%be%89%e4%b8%89%e8%a7%92","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=999","title":{"rendered":"LeetCode 118 &#8211; \u6768\u8f89\u4e09\u89d2"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570 numRows\uff0c\u751f\u6210\u300c\u6768\u8f89\u4e09\u89d2\u300d\u7684\u524d numRows \u884c\u3002<br \/>\n\u5728\u300c\u6768\u8f89\u4e09\u89d2\u300d\u4e2d\uff0c\u6bcf\u4e2a\u6570\u662f\u5b83\u5de6\u4e0a\u65b9\u548c\u53f3\u4e0a\u65b9\u7684\u6570\u7684\u548c\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165: numRows = 5<br \/>\n\u8f93\u51fa: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u6bcf\u884c\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u6570\u5b57\u90fd\u662f1\uff0c\u800c\u4e2d\u95f4\u4f4d\u7f6e col \u53ef\u4ee5\u7528\u4e0a\u4e00\u884c\u7684 col &#8211; 1 \u548c col \u52a0\u5728\u4e00\u8d77\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public List&lt;List&lt;Integer&gt;&gt; generate(int numRows) {\n    List&lt;List&lt;Integer&gt;&gt; ans = new ArrayList&lt;&gt;();\n    for (int row = 0; row &lt; numRows; row++) {\n        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n        for (int col = 0; col &lt;= row; col++) {\n            if (col == 0 || col == row) {\n                list.add(1);\n            } else {\n                int num = ans.get(row - 1).get(col - 1) + ans.get(row - 1).get(col);\n                list.add(num);\n            }\n        }\n        ans.add(list);\n    }\n    return ans;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun generate(numRows: Int): List&lt;List&lt;Int&gt;&gt; {\n    val ans = ArrayList&lt;List&lt;Int&gt;&gt;()\n    for (row in 0 until numRows) {\n        val list = ArrayList&lt;Int&gt;()\n        for (col in 0..row) {\n            if (col == 0 || col == row) {\n                list.add(1)\n            } else {\n                val num = ans[row - 1][col - 1] + ans[row - 1][col]\n                list.add(num)\n            }\n        }\n        ans.add(list)\n    }\n    return ans\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570 numRows\uff0c\u751f\u6210\u300c\u6768\u8f89\u4e09\u89d2\u300d\u7684\u524d numRows \u884c\u3002 \u5728\u300c\u6768\u8f89\u4e09\u89d2\u300d\u4e2d\uff0c\u6bcf\u4e2a [&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,40],"class_list":["post-999","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-40"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/999","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=999"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/999\/revisions"}],"predecessor-version":[{"id":1000,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/999\/revisions\/1000"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}