(Leetcode) Pascal ' s Triangle---yang Hui triangle

Source: Internet
Author: User

Given numrows, generate the first numrows of Pascal ' s triangle.

For example, given numrows = 5,
Return

[     1], [    1,2,1], [   1,3,3,1], [  1,4,6,4,1]

Subscribe to see which companies asked this question

Problem Solving Analysis:

The subject of this Pascal (1623----1662) was discovered in 1654, 393 years later than Yang Hui, 600 years later than Jia Xian.

The concrete algorithm is as follows, we discuss in two kinds of situations,

1, the first element [1].

2, starting from the second element to match the sum of the above results, and finally at the end of a 1

Then we first result.append ([1]). Then we make a judgment and execute the formula:

Ans = result[i].append (Result[i-1][j] + result[i-1][j-1])


#-*-Coding:utf-8-*-__author__ = ' Jiuzhang ' class solution (object):    def generate (self, numrows):        result = []
   for i in range (numrows):            result.append ([1]) for            J in range (1, i + 1):                if J = = 1:                    result.append (1)                El SE:                    result[i].append (Result[i-1][j] + result[i-1][j-1])        return result


(Leetcode) Pascal ' s Triangle---yang Hui triangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.