{"id":8836,"date":"2021-11-27T20:49:43","date_gmt":"2021-11-28T04:49:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8836"},"modified":"2021-11-27T21:01:30","modified_gmt":"2021-11-28T05:01:30","slug":"leetcode-119-pascals-triangle-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-119-pascals-triangle-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 119. Pascal&#8217;s Triangle II"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>rowIndex<\/code>, return the&nbsp;<code>rowIndex<sup>th<\/sup><\/code>&nbsp;(<strong>0-indexed<\/strong>) row of the&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> rowIndex = 3\n<strong>Output:<\/strong> [1,3,3,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> rowIndex = 0\n<strong>Output:<\/strong> [1]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rowIndex = 1\n<strong>Output:<\/strong> [1,1]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= rowIndex &lt;= 33<\/code><\/li><\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Could you optimize your algorithm to use only&nbsp;<code>O(rowIndex)<\/code>&nbsp;extra space?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Remember last row<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n)<\/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<int> getRow(int rowIndex) {\n    vector<int> ans(1, 1);\n    for (int i = 2; i <= rowIndex + 1; ++i) {\n      vector<int> tmp(i, 1);\n      for (int j = 1; j < i - 1; ++j)\n        tmp[j] = ans[j] + ans[j - 1];\n      swap(ans, tmp);\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\/simulation\/leetcode-118-pascals-triangle\/\" data-type=\"post\" data-id=\"8833\">\u82b1\u82b1\u9171 LeetCode 118. Pascal\u2019s Triangle<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;rowIndex, return the&nbsp;rowIndexth&nbsp;(0-indexed) row of the&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":[46],"tags":[18,222,179],"class_list":["post-8836","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8836","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=8836"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8836\/revisions"}],"predecessor-version":[{"id":8842,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8836\/revisions\/8842"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}