{"id":8833,"date":"2021-11-27T20:44:04","date_gmt":"2021-11-28T04:44:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8833"},"modified":"2021-11-27T20:50:56","modified_gmt":"2021-11-28T04:50:56","slug":"leetcode-118-pascals-triangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-118-pascals-triangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 118. Pascal&#8217;s Triangle"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>numRows<\/code>, return the first numRows of&nbsp;<strong>Pascal&#8217;s triangle<\/strong>.<\/p>\n\n\n\n<p>In&nbsp;<strong>Pascal&#8217;s triangle<\/strong>, each number is the sum of the two numbers directly above it as shown:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/0\/0d\/PascalTriangleAnimated2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numRows = 5\n<strong>Output:<\/strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numRows = 1\n<strong>Output:<\/strong> [[1]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= numRows &lt;= 30<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n<sup>2<\/sup>)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> generate(int numRows) {\n    vector<vector<int>> ans(numRows);\n    for (int i = 0; i < numRows;++i) {\n      ans[i].resize(i + 1);\n      ans[i][0] = ans[i][i] = 1;\n      for (int j = 1; j < i; ++j)\n        ans[i][j] = ans[i-1][j] + ans[i-1][j-1];\n    }\n    return ans;\n  }\n}\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-119-pascals-triangle-ii\/\" data-type=\"post\" data-id=\"8836\">\u82b1\u82b1\u9171 LeetCode 119. Pascal\u2019s Triangle II<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;numRows, return the first numRows of&nbsp;Pascal&#8217;s triangle. In&nbsp;Pascal&#8217;s triangle, each number is the sum of the two numbers directly above it as shown:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[18,222,179],"class_list":["post-8833","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-dp","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8833"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8833\/revisions"}],"predecessor-version":[{"id":8840,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8833\/revisions\/8840"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}