{"id":139,"date":"2017-08-02T20:45:38","date_gmt":"2017-08-02T12:45:38","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=139"},"modified":"2018-11-27T16:49:45","modified_gmt":"2018-11-27T08:49:45","slug":"119-pascals-triangle-ii","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=139","title":{"rendered":"119. Pascal&#8217;s Triangle II"},"content":{"rendered":"<div class=\"content__eAC7\">\n<div>\n<p>Given a non-negative\u00a0index\u00a0<em>k<\/em>\u00a0where\u00a0<em>k<\/em>\u00a0\u2264\u00a033, return the\u00a0<em>k<\/em><sup>th<\/sup>\u00a0index row of the Pascal&#8217;s triangle.<\/p>\n<p>Note that the row index starts from\u00a00.<\/p>\n<p><img src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/0\/0d\/PascalTriangleAnimated2.gif\" alt=\"\" \/><br \/>\n<small>In Pascal&#8217;s triangle, each number is the sum of the two numbers directly above it.<\/small><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><strong>Input:<\/strong> 3\r\n<strong>Output:<\/strong> [1,3,3,1]\r\n<\/pre>\n<p><strong>Follow up:<\/strong><\/p>\n<p>Could you optimize your algorithm to use only\u00a0<em>O<\/em>(<em>k<\/em>) extra space?<\/p>\n<\/div>\n<\/div>\n<div class=\"css-12aggky\">\n<div class=\"css-oqu510\">\n<div class=\"css-y3si18\">Accepted<\/div>\n<\/div>\n<\/div>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def getRow(self, rowIndex):\r\n        &quot;&quot;&quot;\r\n        :type rowIndex: int\r\n        :rtype: List[int]\r\n        &quot;&quot;&quot;\r\n        rowIndex +=1\r\n        row_list = []\r\n        for i in range(1, rowIndex+1):\r\n            row_list.append(self.return_result(i,rowIndex))\r\n        return row_list\r\n\r\n\r\n    def return_result(self, a, row_num):\r\n        return self.factorial(row_num-1)\/(self.factorial(a-1)*self.factorial(row_num-a))\r\n\r\n    def factorial(self, n):\r\n        return reduce(lambda x, y: x * y, [1] + range(1, n + 1))\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-negative\u00a0index\u00a0k\u00a0where\u00a0k\u00a0\u2264\u00a033, return the\u00a0k<a href=\"https:\/\/www.ccagml.com\/?p=139\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">119. Pascal&#8217;s Triangle II<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/139"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/139\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}