{"id":137,"date":"2017-09-02T20:35:14","date_gmt":"2017-09-02T12:35:14","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=137"},"modified":"2018-11-27T16:36:18","modified_gmt":"2018-11-27T08:36:18","slug":"118-pascals-triangle","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=137","title":{"rendered":"118. Pascal&#8217;s Triangle"},"content":{"rendered":"<p>Given a non-negative integer\u00a0<em>numRows<\/em>, generate the first\u00a0<em>numRows<\/em>\u00a0of Pascal&#8217;s triangle.<\/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> 5\r\n<strong>Output:<\/strong>\r\n[\r\n     [1],\r\n    [1,1],\r\n   [1,2,1],\r\n  [1,3,3,1],\r\n [1,4,6,4,1]\r\n]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def generate(self, numRows):\r\n        &quot;&quot;&quot;\r\n        :type numRows: int\r\n        :rtype: List[List[int]]\r\n        &quot;&quot;&quot;\r\n        rows_list = []\r\n        for i in range(1, numRows+1):\r\n            rows_list.append(self.return_row(i))\r\n        return rows_list\r\n\r\n\r\n    def return_row(self, row_num):\r\n        row_list = []\r\n        for i in range(1, row_num+1):\r\n            row_list.append(self.return_result(i,row_num))\r\n        return row_list\r\n\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<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-negative integer\u00a0numRows, generate the firs<a href=\"https:\/\/www.ccagml.com\/?p=137\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">118. Pascal&#8217;s Triangle<\/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\/137"}],"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=137"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}